Javascript:通过引用而不是通过值传递函数

Javascript:通过引用而不是通过值传递函数

问题描述:

如果我有

var foo = function() {
    console.log('foo');
}, bar = foo;

并调用bar(),控制台将记录foo.如果那样的话,我会

and call bar(), the console logs foo. If then, I do

foo = function() {
    console.log('not foo');
}

并调用bar(),控制台仍会记录foo.我该如何解决?

and call bar(), the console still logs foo. How can I get around this?

您尝试做的事情不是很明智,但是我会把you弹枪交给您,高飞! (我的意思是你的腿.)

what you're trying to do is not very wise, but i'll hand you the shotgun, blast off! (your legs i mean.)

var foo = function () { console.log("foo"); }
var bar = function () { foo(); }
bar();
var foo = function () { console.log("qux"); }
bar();