473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Recursive object literals in Javascript ?

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

Jun 27 '08 #1
6 2041
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

Jun 27 '08 #2
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
Jun 27 '08 #3
J.S.Criptoff wrote on 19 apr 2008 in comp.lang.javascript:
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
You cannot borrow from another language,
[how would you return it?]
most of us do not lisp.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #4
On 19 ÁÐÒ, 12:51, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
J.S.Criptoff wrote on 19 apr 2008 in comp.lang.javascript:
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

You cannot borrow from another language,
[how would you return it?]
most of us do not lisp.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Should I replace word "borrowed" with "stolen"? Anyway "sharp
variables" do work in FF (SpiderMonkey).
Jun 27 '08 #5
Michael Wojcik wrote:
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.
It was Evertjan who posted that suggestion. I think my first suggestion
would have been that this may be a case for using a constructor to
create the object rather than an Object literal. I.E.:-

var x = new SomeObj();

function SomeObj(){
this.b = this;
}
SomeObj.prototype.a = 3;

- which does not involve creating any more additional function objects
than your suggestion, and fewer if there are going to be more than one
such objects created.
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);
The problem with all of these non-standard language extensions is that
while you can employ them when writing for a single known implementation
where they are implemented they are mostly non-practical in a general
context because they mostly constitute a syntax error where not
implemented, and there are virtually no practical steps that can be
taken to detect support for a syntax extension without provoking the
syntax error wherever it does not exist.

Richard.
Jun 27 '08 #6
Richard Cornford wrote:
Michael Wojcik wrote:
>Richard Cornford wrote:
>>java wrote:

var x = {a:3, b:x};

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).

Of course, evaluation of x on the RHS can be deferred by
wrapping it in a closure:

...

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:

...

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.

It was Evertjan who posted that suggestion.
Sorry about the misattribution.
I think my first suggestion
would have been that this may be a case for using a constructor to
create the object rather than an Object literal.
Agreed. In this case I was interested in the OP's question, rather
than better ways to accomplish the same result.
>If you're using an implementation that supports defining
getters in an object literal ...

The problem with all of these non-standard language extensions is that
while you can employ them when writing for a single known implementation
where they are implemented they are mostly non-practical in a general
context because they mostly constitute a syntax error where not
implemented, and there are virtually no practical steps that can be
taken to detect support for a syntax extension without provoking the
syntax error wherever it does not exist.
Agreed again, in the general case. Some of the Javascript I write
explicitly targets specific implementations, just like some of the
programs I write in other languages.

--
Michael Wojcik

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bennett Haselton | last post by:
Is there any way to find a string representing an object's class, which will work in Internet Explorer 6? "typeof" doesn't work -- it returns "object" for all objects: x =...
1
by: Matt | last post by:
Hi all :) I'm trying to get the functionality gained using only CSS in Opera/Gecko etc on this page: <http://matt.blissett.me.uk/web/authoring/css_menus/sample> to work in IE, using javascript....
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
7
by: Eric Laberge | last post by:
Aloha! This question is meant to be about C99 and unnamed compound objects. As I read, if such a construct as int *p = (int){0}; is used within a function, then it has "automatic storage...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
6
by: Jake Barnes | last post by:
I was just reading this article on Ajaxian: http://ajaxian.com/archives/show-love-to-the-object-literal This is a newbie question, but what is the object literal? I thought it was like an...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
6
by: Christoph Boget | last post by:
If I had an array like so: var bob = ; I can find out the size of the array by doing bob.length; Is there a comparable way to get the size of an object? So if I did:
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.