Assignment operator javascript

S

Sister Ray

Hello,
I was wondering what is the difference of declaring a member of an
object like this:

function myobj()
{
var member1 = "somestring";
var member2 = "somestring2";
}

and

function anotherobj()
{
member1 : "somestring";
member2 : "somestring2"
}

best regards,
Carlos Pedro
 
J

Joost Diepenmaat

Sister Ray said:
Hello,
I was wondering what is the difference of declaring a member of an
object like this:

function myobj()
{
var member1 = "somestring";
var member2 = "somestring2";
}

That does not define members/properties of any (user-accessible) object.
and

function anotherobj()
{
member1 : "somestring";
member2 : "somestring2"
}

That is invalid javascript.
 
T

Thomas 'PointedEars' Lahn

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
 
S

Sister Ray

However, perhaps the OP was looking for this constructor:

  function MyObj()
  {
    this.member1 = "somestring";
    this.member2 = "somestring2";
  }
I've tested the

function MyObj()
{
var member1 = "somestring";
var member2 = "somestring2";
}

constructor with firefox and IE7, and i had access to the "members" of
the object, i assume there are other implementations of javascript
where these member would be 'private' and so unaccessible using the .
operator. And so i should add members explicitly using "this.". Am I
correct.
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.
I wrote this my mistake and found it odd that i didn't get a syntax
error, I didn't remember that the member1: would be considered as a
label.
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.

The Objects created with the object initializer, can't be reused?
Using this initalizer are we creating something in the likes of a
Singleton class?

Best Regards,
Carlos Pedro
 
T

Thomas 'PointedEars' Lahn

Sister said:
I've tested the

function MyObj()
{
var member1 = "somestring";
var member2 = "somestring2";
}

constructor with firefox and IE7, and i had access to the "members" of
the object,

Certainly from *within* the constructor or using "public" methods defined
therein. Variables are *properties* of the built-in (and locally
inaccessible) Variable Object. (They do _not_ go by the name "members".)
i assume there are other implementations of javascript
where these member would be 'private'

More like "protected":

function MyObj()
{
var member1 = "somestring";
var member2 = "somestring2";
this.getMember1 = function() { return member1; };
this.setMember1 = function(m1) { member1 = m1; };
}

var o = new MyObj();

// creates a new "public" property
o.member1 = "foo"

// sets the "protected" property
o.setMember1("bar");

// gets the "public" property
console.log(o.member1);

// gets the "protected" property
console.log(o.getMember1());
and so unaccessible using the . operator.

`.' is not specified as an operator, although it could be regarded one.
Regardless, in conforming implementations the scope of local variables does
not extend beyond the local execution context in which they were declared.
With closures, that execution context is being reproduced on call.
And so i should add members explicitly using "this.". Am I correct.
Depends.


The Objects created with the object initializer, can't be reused?

The objects themselves can be reused, of course. My point was instead that
the data structure cannot be reused easily; without further tricks, you
have to keep track of the property names, while in the variant with the
user-defined constructor the constructor can do that for you.
Using this initalizer are we creating something in the likes of a
Singleton class?

In the likes, roughly speaking. You are talking class-based about languages
that use prototypal inheritance; learn the difference.

And, as a concluding request: Please, we have discussed all of this
*ad nauseam* already. RTFM, STFW.

<http://jibbering.com/faq/>
<https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/JavaScript_Overview#What_is_JavaScript.3F>
<http://javascript.crockford.com/javascript.html> pp.
<http://PointedEars.de/es-matrix>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top