Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Assignment operator javascript

Reply
Thread Tools

Assignment operator javascript

 
 
Sister Ray
Guest
Posts: n/a
 
      01-15-2009
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
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      01-15-2009
Sister Ray <(E-Mail Removed)> writes:

> 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.


--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-15-2009
Joost Diepenmaat wrote:

> Sister Ray <(E-Mail Removed)> writes:
>> 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.


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() ...

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

>
> 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
 
Reply With Quote
 
Sister Ray
Guest
Posts: n/a
 
      01-15-2009
On Jan 15, 12:17*pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> 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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-15-2009
Sister Ray wrote:

> Thomas 'PointedEars' Lahn wrote:
>> 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,


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.

>> 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?


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM



Advertisments