2

I want to know if it is possible to declare an array in Javascript of the type "com.peregrine.servicecenter.PWS.Common.MessageType". In java it is easy but in javascript I have not idea. Thanks.

user807623
  • 21
  • 1
  • 3
  • Is your question really "How do I declare something in *JavaScript* that is an instance of a particular *Java* class?" You do understand that Java and JavaScript are separate languages? – nnnnnn Jun 21 '11 at 02:28
  • Yes, I know they are separate but the question is about if you can "simulate" a java class array in Javascript. Thanks – user807623 Jun 21 '11 at 14:44
  • @nnnnnn Java is to JavaScript as Car is to Carpet. :-) (I know you know the difference user, but I couldn't help it) – cwallenpoole Jun 21 '11 at 14:48
  • @cwallenpoole - yes, I know the difference too. But the way the question was phrased there was no mention of simulating Java's package concept using JavaScript, or namespacing or whatever, it asked about declaring a variable of a specific type, "com.peregrine.servicecenter.etc...", which actually is a library available for Java. – nnnnnn Jun 21 '11 at 23:31
  • you should update your question to say what you really mean. Even from your last comment it is not clear if you want to be able to simulate the specific Java class you mentioned, or do the namespace thing, "a.b.c.d.etc", or create a JavaScript array that is restricted to holding objects of a certain type, or... – nnnnnn Jun 21 '11 at 23:34
  • Check out this thread; it basically asks the same question: http://stackoverflow.com/questions/881515/javascript-namespace-declaration – Jeff Jun 21 '11 at 01:48
  • I've never worked with namespaces but I'm going to read about it, thanks. – user807623 Jun 21 '11 at 14:31

4 Answers4

2

sure it's possible:

var myArray = [];

remember that javascript is not a statically typed language, so you don't need to declare an array of a specific type.... just an array. Now, given the type you're referring to, I don't think that's exactly what you're asking though...

Jaime
  • 6,736
  • 1
  • 26
  • 42
0

No, it is not possible. The Array in JS doesn't care what you've put in it, or even that the array indexes are numeric. Java, on the other hand, requires strict typing of both. I'd even go so far as to say that even Object[] is a completely different paradigm from [].

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

You cannot declare an array to exclusively consist of a particular type. However, you can declare an array (var myArray = [];) and you can add objects of your intended type to it (myArray.push(myMessageType);).

brymck
  • 7,555
  • 28
  • 31
0

You can declare the array in such a way.

 var arr = []; 

it can contain object or array of object by calling

 arr.push(x)

Reference

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array

Array functions in jQuery

Community
  • 1
  • 1
Mangesh
  • 3,987
  • 2
  • 31
  • 52