473,394 Members | 1,802 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,394 software developers and data experts.

function literals

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 :)
Jul 27 '05 #1
6 1312
>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]);
}

Jul 27 '05 #2
Baconbutty wrote:
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.
Jul 27 '05 #3
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).

Jul 27 '05 #4
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>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 27 '05 #6
Baconbutty wrote:
See

http://jibbering.com/faq/faq_notes/closures.html


Thanks. I will read it and will come back if I have any questions left
about it :)
Jul 27 '05 #7

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

Similar topics

3
by: Gary Besta | last post by:
I am trying to add a simple case statement to a stored procedure or user defined function. However when I try and save the function/procedure I get 2 syntax errors. Running the query in query...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
10
by: Pedro Pinto | last post by:
Hi there! I'm creating a function that copies some information to a buffer and enters a delimiter string "//" between results. My issue here is when i print the buffer it appears this weird...
3
by: sam_cit | last post by:
Hi Everyone, I have the following function, char* sample1() { char *p = "india"; return(p); }
10
by: Jess | last post by:
Hello, If a function that returns an array of char like this one: const char* f(){ return "abc"; } then is the char array "abc" local/temporary object? I thought it should be temporary...
8
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
7
by: jacklisp | last post by:
I have wrote a small function but got the error function: /*< squeeze */ void squeeze(char s1,char s2) { int de_s ; int i,j,k; for(i = 0;s2 !='\0';++i) {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.