function literals

R

Robert

Hi,
I don't really have a problem, but I would like to know what is
happening when using function literals for events.
For example:

var obj = {value: 5};
var val = obj.value;
button.onclick = function()
{
alert(obj.value);
alert(val);
}

Clicking will display 5 and 5 of course.
If some script later will do
obj.value++;
the clicking will display 6 and 5.

Could someone tell me how 'obj.value' and 'val' are resolved in the
function literal?
I have some ideas... but like to be sure. Because I keep feeling
insecure when using function literals :)
 
B

Baconbutty

var obj={value:5}

That is an "object" literal, equivalent to:-

var obj=new Object();
obj.value=5;
var val = obj.value;

Numbers, Strings and Booleans are generally passed by "value" not by
"reference", i.e. *copied* rather than *linked to*.

So as "obj.value" is a Number, "var val=obj.value" *copies* into the
variable "val", the value of obj.value *at that point* in the
execution. It does not link "val" to "object.value" in any way. If
you later update "object.value" it will have no effect on "val".

Hence if you update object.value later on, val will not change.

Contrast this with Arrays and Objects, which are passed by "reference".

E.g.

var obj = {value: new Array()};
obj.value[0]=5;
var val = obj.value; // val contains a reference to Array

button.onclick = function()
{
obj.value[0]++;

alert(obj.value[0]);
alert(val[0]);
}
 
R

Robert

Baconbutty said:
Numbers, Strings and Booleans are generally passed by "value" not by
"reference", i.e. *copied* rather than *linked to*.

Unfortunately I did not seem to be clear enough in my question, because
this I do know.

I forgot to mention that the next code is executed in another function,
so that the object 'obj' does not seem to be in the scope when the
onclick executes.

function test()
{
var obj = {value: 5};
var val = obj.value;
button.onclick = function()
{
alert(obj.value);
alert(val);
}
}

So I would like to know when and how the 'obj.value' is resolved.
 
B

Baconbutty

You need to research "closures" and "execution contexts".

My technical knowledge on this is limited, and others will no doubt
give more complete answers.

However, in practice:-

- When test() is run, "obj" will have scope in both test() and the
anonymous function. Effectively "obj" is a *global* for the anonymous
function.

- A "closure" means that *in some way* the anonymous function is able
to continue to access "obj" (and "val" for that matter).
 
M

Michael Winter

On 27/07/2005 11:15, Robert wrote:

[snip]
I forgot to mention that the next code is executed in another function,
so that the object 'obj' does not seem to be in the scope when the
onclick executes.

It is, but explaining how it works properly requires a lot of detail.
Whilst I don't have any problem with that, closures[1] are covered in
the FAQ notes[2].

Have a read. If there are problems, just ask, but please be specific,
including what you do understand so that can be omitted.

[snipped code]

Mike


[1] <URL:http://www.jibbering.com/faq/faq_notes/closures.html>
[2] <URL:http://www.jibbering.com/faq/faq_notes/faq_notes.html>
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top