Javascript Function-Pointer Assignment

前端 未结 11 1056
广开言路
广开言路 2020-12-04 12:05

Consider this javascript code:

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

When runn

相关标签:
11条回答
  • 2020-12-04 12:30

    You are assigning 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, not copy.

    Here are some examples:

    "obj2" is a reference of "obj1", you change "obj2", and "obj1" is changed. It will alert false.

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

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

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

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

    var obj1 = {subObj:{prop:true}},
        obj2 = obj1.subObj;
    obj2.prop = false;
    alert(obj1.subObj.prop);
    
    0 讨论(0)
  • 2020-12-04 12:31

    Yes, there's nothing special about the fact that the variables are referring to functions, there's no aliasing involved.

    var bar = 1;
    var foo = bar;
    bar = "something entirely different";
    // foo is still 1
    
    0 讨论(0)
  • 2020-12-04 12:36

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

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

    creates an anonymous function that alerts "A" on execution;

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

    assign that function to bar;

    var foo = bar;
    

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

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

    rebind bar to an anonymous function "B". This won't affect foo or the other function "A".

    foo();
    

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


    Actually in languages where there are function points e.g. C it won't affect foo either. I don't 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"
    
    0 讨论(0)
  • 2020-12-04 12:37

    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();
    
    0 讨论(0)
  • 2020-12-04 12:38

    Yes that is expected and by design.

    Your question is basically: does foo reference bar as a pointer or reference would in another language?

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

    0 讨论(0)
  • 2020-12-04 12:40

    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 instantiating FunctionDeclaration f as described in Clause 13.

    Let funcAlreadyDeclared be the result of calling env’s HasBinding concrete method passing fn as the argument.

    If funcAlreadyDeclared is false, call env’s CreateMutableBinding concrete method passing fn and configurableBindings as the arguments.

    References

    • ECMAScript-5: Section 10.5
    0 讨论(0)
提交回复
热议问题