nested literal objects

Discussion in 'Javascript' started by przemek.ch, Feb 12, 2010.

  1. przemek.ch

    przemek.ch Guest

    Hi,

    example first

    var a = {
    b : {
    x : " test"
    }
    };


    a.someMethod = function(){
    alert(this.b.x);
    }

    This code works but...
    I was wondering about few things

    1) eclipse jsdt validator shows that
    this.b.x - x should not be accessed in static way - why?

    2) is that a correct way of object declaration? especially that nested
    b object
    przemek.ch, Feb 12, 2010
    #1
    1. Advertising

  2. przemek.ch

    Scott Sauyet Guest

    On Feb 12, 8:20 am, "przemek.ch" <> wrote:
    [ abbreviated example:]
    > var a = {b: {x: " test"}};
    > a.someMethod = function(){alert(this.b.x);}
    >
    > This code works but... I was wondering about few things
    >
    > 1) eclipse jsdt validator shows that
    >     this.b.x   - x should not be accessed in static way - why?


    No idea why it says that, but it's wrong in this case.

    > 2) is that a correct way of object declaration? especially that nested
    > b object


    Yes, this is the most straightforward method, probably the most
    efficient, and certainly the most readable.

    -- Scott
    Scott Sauyet, Feb 12, 2010
    #2
    1. Advertising

  3. przemek.ch wrote:
    ^^^^^^^^^^
    I usually don't talk to domains. Please put a name there, preferably a
    real one.

    > example first
    >
    > var a = {
    > b : {
    > x : " test"
    > }
    > };
    >
    >
    > a.someMethod = function(){
    > alert(this.b.x);


    Should be

    window.alert(this.b.x);

    (See previous discussions.)

    > }
    >
    > This code works but...
    > I was wondering about few things
    >
    > 1) eclipse jsdt validator shows that
    > this.b.x - x should not be accessed in static way - why?


    AISB, previous versions of JSDT were written by IBM developers with, it
    appeared so, primarly Java background and little understanding of
    ECMAScript, trying to validate against a lot of concepts of Java that do
    not apply to ECMAScript implementations, even if user preferences
    suppressed the corresponding warnings or error messages.

    Therefore, I suggest you update to at least JSDT 1.2.0¹ where handling of
    ObjectLiterals was considerably improved (including the Outline View).
    Enabling all warnings for JavaScript validation, the only warnings I get
    there are:

    - "Non-externalized string literal" in line 3
    - "Missing semicolon" in line 9

    I am using the following JSDT Validator default settings instead:

    * Errors/Warnings:
    Everything set to "Warning" except the following set to "Ignore":
    - Parameter assignment
    - Non-externalized strings
    - Uninitialized global variables
    - Local variable declaration hides another field or variable
    * JSDoc:
    - [x] Process JSDoc comments
    - Malformed JSDoc comments: Warning
    - Only consider members as visible as: Default
    - [x] Report errors in tags
    - [_] Report non-visible references
    - [x] Report deprecated references
    - Missing JSDoc tags: Ignore
    - Missing JSDoc comments: Ignore

    These defaults avoided spurious warnings and error messages with previous
    JSDT versions, so some of them might be worth modifying with newer version
    (however, I can still find nothing wrong in assigning to arguments as it
    saves me one variable declaration for each default argument value; YMMV).

    > 2) is that a correct way of object declaration? especially that nested
    > b object


    Objects are *not* declared, they are _constructed_. Objects have an
    identity, not a name.

    Here you are declaring the _variable_ `a' which, after the assignment,
    holds a _reference_ to a newly constructed object (which `b' _property_
    holds a _reference_ to another newly constructed object that has an `x'
    property that holds a primitive string value).

    And yes, this code is syntactically correct in many implementations (try it
    out).³ Whether it is also semantically correct depends on your use case.


    HTH

    PointedEars
    ___________
    ¹ org.eclipse.wst.jsdt.feature.feature.group
    1.2.0.v200912170603-771FEcCcNBEbBTMQCXO_
    in EPP PHP Feature (org.eclipse.epp.package.php.feature.feature.group)
    1.3.0.20100201-1819
    in Eclipse IDE for PHP Developers (epp.package.php) 1.3.0.20100201-1819
    for Eclipse 3.6M5+ (Helios; I installed EPP for M5¹ but did a full update
    afterwards, so this JSDT version might be for M6)
    ² <http://www.eclipse.org/downloads/>, "Development" tab
    ³ <http://PointedEars.de/es-matrix>
    --
    Danny Goodman's books are out of date and teach practices that are
    positively harmful for cross-browser scripting.
    -- Richard Cornford, cljs, <cife6q$253$1$> (2004)
    Thomas 'PointedEars' Lahn, Feb 12, 2010
    #3
  4. Stefan Weiss wrote:

    > The warning is likely bogus. I don't use Eclipse with JSDT, so I can't
    > advise how to avoid it. You could try using alert(a.b.x), but only if
    > you don't plan on using someMethod in a different context. Does the
    > warning still occur if someMethod is included in the object literal?
    >
    > var a = {
    > b: {
    > x: "test"
    > },
    > someMethod = function(){

    ^
    > alert(this.b.x);
    > }
    > };


    No, a syntax error occurs instead ;-)


    SCNR

    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Feb 12, 2010
    #4
    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. Johannes
    Replies:
    0
    Views:
    321
    Johannes
    Oct 7, 2004
  2. Moosebumps

    "literal" objects

    Moosebumps, Dec 24, 2003, in forum: Python
    Replies:
    9
    Views:
    293
    Pekka Pessi
    Jan 3, 2004
  3. Stephen Miller
    Replies:
    1
    Views:
    286
    Alessandro Zifiglio
    Jan 2, 2004
  4. Stephen Miller
    Replies:
    12
    Views:
    763
    Stephen Miller
    Jan 20, 2004
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    374
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page