|
Prev: What is a findName reference?
Next: FAQ Topic - How can I disable the back button in a web browser? (2008-04-17)
From: java on 16 Apr 2008 18:58 Consider: var x = {a:3, b:x}; alert(x.b) ==> undefined Question: why ? x is a pointer to an object, so x.b should refer to that same object. Theoretically: x --> some object containing a = 3, b --> x However, this is NOT the case. So why is x.b "undefined" in JS ? --j
From: Richard Cornford on 16 Apr 2008 19:27 java wrote: > Consider: > > var x = {a:3, b:x}; > > alert(x.b) ==> undefined > > Question: why ? > > x is a pointer to an object, so x.b should refer > to that same object. > > Theoretically: > x --> some object containing > a = 3, > b --> x > > However, this is NOT the case. So why is > x.b "undefined" in JS ? The order of evaluation. The right hand side of an assignment expression is evaluated before the resulting value can be assigned to the left hand side (it has to be). So at the point of evaluation the object literal the - x - variable has its default - undefined - value, and so that is what bets assigned to the object's - b - property. Richard.
From: Evertjan. on 17 Apr 2008 05:07 Richard Cornford wrote on 17 apr 2008 in comp.lang.javascript: > java wrote: >> Consider: >> >> var x = {a:3, b:x}; >> >> alert(x.b) ==> undefined >> >> Question: why ? >> >> x is a pointer to an object, so x.b should refer >> to that same object. >> >> Theoretically: >> x --> some object containing >> a = 3, >> b --> x >> >> However, this is NOT the case. So why is >> x.b "undefined" in JS ? > > The order of evaluation. The right hand side of an assignment > expression is evaluated before the resulting value can be assigned to > the left hand side (it has to be). So at the point of evaluation the > object literal the - x - variable has its default - undefined - value, > and so that is what bets assigned to the object's - b - property. Right. =========== simply doing: var x = {a:3}; x.b = x; alert(x.b.a); // 3 could get interesting: var x = {a:3}; x.b = x; alert(x.b.b.b.b.b.b.b.b.b.b.b.b.a); // 3 and perhaps even useful in visonary recursionistic programming? -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: Michael Wojcik on 18 Apr 2008 13:22 Richard Cornford wrote: > java wrote: >> Consider: >> >> var x = {a:3, b:x}; >> >> alert(x.b) ==> undefined > > The right hand side of an assignment expression > is evaluated before the resulting value can be assigned to the left hand > side (it has to be). So at the point of evaluation the object literal > the - x - variable has its default - undefined - value, and so that is > what bets assigned to the object's - b - property. Of course, evaluation of x on the RHS can be deferred by wrapping it in a closure: var x = {a:3, b:function(){return x;}}; alert(x.b().a); Obviously that's not the same thing as what the OP was trying to do, but it's a common idiom in functional programming. Alternatively, with an implementation that supports getters, you can provide a getter for b that returns x: var x = {a:3}; x.__defineGetter__("b",function(){return this;}); alert(x.b.a); though whether that's good practice is debatable. (And in this particular case, I don't think this has any advantage this has over simply "x.b=x", as Richard suggested. If you wanted b to evaluate to x.a, then the getter would do something for you.) If you're using an implementation that supports defining getters in an object literal (eg sufficiently recent Mozilla Javascript), then you can create a closure that will automatically be evaluated, as part of your object literal: var x = {a:3, get b(){return this;}}; alert(x.b.a); which achieves the same result as the OP's example. x.b is not actually a reference to x, of course; it just returns one when evaluated. Though I'm not sure whether a Javascript program can distinguish the two. -- Michael Wojcik
From: J.S.Criptoff on 18 Apr 2008 18:32
With an implementation that supports "sharp variables" (non-standard notation borrowed from Common Lisp) we can write: var x = #1= {a: 3, b: #1#}; alert(x.b.a); //-> 3 |