他们在那里所做的是在不 调用 函数的情况下 引用 该函数。 __
var x = foo; // Assign the function foo to xvar y = foo(); // Call foo and assign its *return value* to y
在Javascript中,函数是对象。正确的对象。这样您就可以传递对它们的引用。
在这种特定情况下,他们正在做的是设置
handleServerResponse为就绪状态更改时XHR对象使用的回调。XHR对象将在执行ajax请求的过程中调用该函数。
其他示例:
// Declare a functionfunction foo() { console.log("Hi there");}// Call itfoo(); // Shows "Hi there" in the console// Assign that function to a variblevar x = foo;// Call it againx(); // Shows "Hi there" in the console// Declare another functionfunction bar(arg) { arg();}// Pass `foo` into `bar` as an argumentbar(foo); // Shows "Hi there" in the console, because `bar` // calls `arg`, which is `foo`它自然地遵循从一个事实,即函数对象,但它的价值呼唤特别是有之间没有神奇的联系
x,并
foo在上面;
它们都是指向相同功能的变量。除了它们指向同一功能外,它们没有任何链接,并且更改一个功能(例如指向另一个功能)对另一个功能没有影响。例:
var f = function() { console.log("a");};f(); // "a"var x = f;x(); // "a"f = function() { console.log("b");};f(); // "b"x(); // "a" (changing `f` had no effect on `x`)


