Assignment operator javascript

Discussion in 'Javascript' started by Sister Ray, Jan 15, 2009.

  1. Sister Ray

    Sister Ray Guest

    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
     
    Sister Ray, Jan 15, 2009
    #1
    1. Advertising

  2. Sister Ray <> 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/
     
    Joost Diepenmaat, Jan 15, 2009
    #2
    1. Advertising

  3. Joost Diepenmaat wrote:

    > Sister Ray <> 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
     
    Thomas 'PointedEars' Lahn, Jan 15, 2009
    #3
  4. Sister Ray

    Sister Ray Guest

    On Jan 15, 12:17 pm, 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, 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
     
    Sister Ray, Jan 15, 2009
    #4
  5. 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
     
    Thomas 'PointedEars' Lahn, Jan 15, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang Groiss
    Replies:
    0
    Views:
    404
    Wolfgang Groiss
    Nov 19, 2003
  2. nagy
    Replies:
    36
    Views:
    1,024
    Terry Reedy
    Jul 20, 2006
  3. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    500
    Barry Schwarz
    Feb 8, 2005
  4. Chris
    Replies:
    34
    Views:
    1,551
  5. Hicham Mouline
    Replies:
    2
    Views:
    709
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page