nested literal objects

P

przemek.ch

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
 
S

Scott Sauyet

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
 
T

Thomas 'PointedEars' Lahn

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

Thomas 'PointedEars' Lahn

Stefan said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top