Javascript Function-Pointer Assignment

前端 未结 11 1094
广开言路
广开言路 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:44

    I would just like to add this also works for pre-defined named functions as well:

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

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

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

    Yes, you've created a pointer to the original "A" function. When you reassign bar, you're reassigning it, but you're still leaving any references to the old function alone.

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

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

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

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

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

    bar and foo are BOTH pointers

    All vars/handles to NON primitive objects in javascript are pointers; pointers ARE native to javascript, they are the 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 run into hidden errors and bugs if you think they are copies. Especially so 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
    

    To really COPY a non-primitive in javascript takes 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
    
    0 讨论(0)
  • 2020-12-04 12:56

    I'm a bit late here but I thought I'd give an answer anyways and flesh something out.

    It's best not to think in terms of pointers and memory references when discussing the internals of JavaScript (or ECMAScript) when dealing with the specifications. Variables are environment records internally and are stored and referenced by name, not memory address. What your assignment statement is doing, internally and by design, is looking up the environment record name (either "foo" or "bar") and assigning the value to that record.

    So,

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

    is assigning the environment record "bar" the value (anonymous function).

    var foo = bar;
    

    internally calls GetValue("bar") which retrieves the value associated with the record "bar" and then associates that value with the record "foo". Hence, afterwards the original value of bar can still be used as it's now associated with foo.

    Because JavaScript references by string and not memory address is precisely why you can do things like this:

    someObject["someProperty"]
    

    which is looking up the value based on the property name.

    0 讨论(0)
提交回复
热议问题