Joost said:
That does not define members/properties of any (user-accessible) object.
Depends on the implementation. It is not accessible by the caller in
conforming implementations of ECMAScript. IIRC, there have been
non-conforming implementations that allowed
myobj.member1
and the like. (See also the "inner functions" bug present there.)
However, perhaps the OP was looking for this constructor:
function MyObj()
{
this.member1 = "somestring";
this.member2 = "somestring2";
}
... new MyObj() ...
That is invalid javascript.
As far as "javascript" is to be understood as JavaScript 1.2+, JScript 3.0+,
or an implementation of ECMAScript Edition 3, it isn't syntactically
invalid; we would be looking at two LabelledStatements here. But it
certainly isn't doing anything overly useful when called.
However, perhaps the OP was looking for
var anotherobj = {
member1: "somestring",
member2: "somestring2"
};
instead. In that case, the difference between the first assumed code and
the second one would be reusability and compatibility:
With the constructor, one can create any number of objects that initially
can (but not must) have the same properties (but not necessarily the same
property values, as arguments can be passed to the constructor), and
inherit from MyObj.prototype. The Object initializer (barring further
manipulation of the object's prototype chain) allows user-defined objects
only to inherit from Object.prototype.
Also, the constructor is fully backwards-compatible, while the Object
initializer requires at least JavaScript 1.3, JScript 3.0, or an
implementation of ECMAScript Ed. 3.
PointedEars