Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > nested literal objects

Reply
Thread Tools

nested literal objects

 
 
przemek.ch
Guest
Posts: n/a
 
      02-12-2010
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
 
Reply With Quote
 
 
 
 
Scott Sauyet
Guest
Posts: n/a
 
      02-12-2010
On Feb 12, 8:20*am, "przemek.ch" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-12-2010
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$(E-Mail Removed)> (2004)
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-12-2010
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$(E-Mail Removed)>
 
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
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
combining nested properties with literal content Johannes ASP .Net 0 10-07-2004 11:32 AM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM
Problem overriding render method to format literal content of nested tags in custom control Stephen Miller ASP .Net Building Controls 12 01-20-2004 08:52 AM
Rendering literal and serveral controls nested in web control at design time Stephen Miller ASP .Net Building Controls 1 01-02-2004 01:26 AM



Advertisments