Javascript pointer function assignment

Consider this javascript code:

var bar = function () { alert("A"); } var foo = bar; bar = function () { alert("B"); }; foo(); 

When I run this code, I get "A". Is this behavior part of the javascript specification and can I rely on it?

+42
javascript function-pointers
Feb 24 '10 at 12:53
source share
11 answers

Yes, which is expected by design.

Your question is basically: foo link bar as a pointer or link in another language?

Answer: no: the value of bar at the time of assignment is assigned to foo .

+35
Feb 24 '10 at
source share
— -

In other examples, nothing was taken by meaning; everything was passed by reference.

bar and foo - BOTH pointers

All vars / handle for NON primitive objects in javascript are pointers; ARE pointers are native to javascript, they are by default.

 var bar = function () { alert("A"); } //bar is a pointer to function1 var foo = bar; //pointer copied; foo is now also a pointer to function1 bar = function () { alert("B"); }; //bar points to function2 foo(); //foo is still a pointer to function1 

You will encounter hidden errors and errors if you think that these are copies. Especially if you work with complex objects. for example

 function person(name){this.name = name} var john = new person("john") var backup = john backup.name //john john.name = "jack" backup.name //jack, NOT john 

Actually COPY non-primitive in javascript requires more work than just a = b. For example:

 function person(name){ this.name = name} var john = new person("john") var backup = new Object() backup = JSON.parse(JSON.stringify(john)) backup.__proto__ = john.__proto__ //useful in some cases john.name = "jack" backup.name //john 
+53
Jul 22 '12 at 6:18
source share

I'm a little late here, but I thought I'd give an answer anyway and come up with something.

It is better not to think in terms of pointers and memory references when discussing the internal components of JavaScript (or ECMAScript) when working with specifications. Variables are records of the environment inside and are stored and referenced by name, not by memory address. What your assignment operator does, both internally and by design, looks for the name of the environment record ("foo" or "bar") and assigns the value to that record.

So,

 var bar = function () { alert("A"); } 

assigns the value "bar" for recording the environment (anonymous function).

 var foo = bar; 

internally calls GetValue ("bar"), which retrieves the value associated with the record "bar", and then associates this value with the record "foo". Therefore, subsequently, the original value of the bar can still be used, since it is now associated with foo.

Since JavaScript links are line by line, not memory address, that’s why you can do things like this:

 someObject["someProperty"] 

which searches for a value based on a property name.

+18
Feb 24 '10 at 19:35
source share

Yes, there is nothing special in the fact that variables refer to functions, there is no smoothing.

 var bar = 1; var foo = bar; bar = "something entirely different"; // foo is still 1 
+5
Feb 24 '10 at
source share

You assign the value of an anonymous function to a variable, not a pointer.
If you want to play with pointers, you can use objects that are passed by reference, rather than copying.

Here are some examples:

"obj2" is the link "obj1", you change "obj2", and "obj1" changes. It will warn false .

 var obj1 = {prop:true}, obj2 = obj1; obj2.prop = false; alert(obj1.prop); 

"prop" indicates a property that is not an object, "prop" is not a pointer to this object, but a copy. If you change "prop", "obj1" will not be changed. It will warn true

 var obj1 = {prop:true}, prop = obj1.prop; prop = false; alert(obj1.prop); 

"obj2" is a reference to the property "subObj" "obj1". if "obj2" is changed, "obj1" is changed. It will warn false .

 var obj1 = {subObj:{prop:true}}, obj2 = obj1.subObj; obj2.prop = false; alert(obj1.subObj.prop); 
+5
Feb 24 '10 at 13:26
source share

Yes, this is the correct behavior.

 //create variable bar and assign a function to it var bar = function () { alert("A"); } //assign value of bar to the newly created variable foo var foo = bar; //assign a new function to the variable bar //since foo and bar are not pointers, value of foo doesn't change bar = function () { alert("B"); }; //call the function stored in foo foo(); 
+3
Feb 24 '10 at 12:59
source share

These are not function pointers (and there are no pointers in JS). Functions in JS can be anonymous and are first class objects. Hence

 function () { alert("A"); } 

creates an anonymous function that warns "A" of execution;

 var bar = function () { alert("A"); }; 

assign this function to bar;

 var foo = bar; 

assign foo to bar, which is the function "A".

 bar = function () { alert("B"); }; 

for the function of the anonymous function "B". This will not affect foo or other "A" function.

 foo(); 

Call the function stored in foo, which is function "A".




Actually in languages ​​where there are functional points, for example. C he will not affect foo . I do not know where you get the idea of ​​getting "B" on reassignment.

 void A(void) { printf("A\n"); } void B(void) { printf("B\n"); } typedef void(*fptr_t)(void); fptr_t foo = A; fptr_t bar = foo; bar = B; foo(); // should print "A" 
+2
Feb 24 '10 at 13:00
source share

This is a variable assignment to an unnamed function, not a function pointer

+1
Feb 24 '10 at 12:57
source share

Yes, you created a pointer to the original function "A". When you reassign a panel, you reassign it, but you still leave links to the old function alone.

So, to answer your question, yes, you can rely on it.

+1
Feb 24 '10 at
source share

I would like to add this, it also works for predefined named functions:

 function myfunc() { alert("A"); } var bar = myfunc; var foo = bar; bar = function () { alert("B"); }; foo(); 

This will do the same, indicating that function names act as array names (pointers).

0
Dec 03 '13 at 18:55
source share

For each FunctionDeclaration f in code in source text order, do:

Let fn be the Identifier in FunctionDeclaration f.

Let fo be the result of an instance of FunctionDeclaration f, as described in section 13.

Let funcAlreadyDeclared be the result of calling the envs HasBinding of a specific method, passing fn as an argument.

If funcAlreadyDeclared is false, call envs CreateMutableBinding a specific method, passing fn and configurableBindings as arguments.

References

0
Mar 27 '14 at 2:09
source share



All Articles