设置一个变量等于没有括号功能?括号、变量、功能

2023-09-11 22:28:19 作者:恶痞

我下面就AJAX的教程,而这个人做的视频做了一些奇怪的事情。至少我以前从来没有见过的。他们设定一个对象的属性相当于一个函数名,但不熟悉的()。后来,他又到定义的功能。 code下面提供了背景。无论如何,这是什么意思设置的东西等于一个函数没有参数?该行code的确是运行时所命名的,你可以看到下面的功能。

I was following a tutorial on AJAX, and the person making the video did something strange. At least I hadn't seen it before. They set an object property equal to a function name, but without the familiar (). He later went on to define the function. Code is provided below for context. Anyway, what does it mean to set something equal to a function without the parameters? That line of code is indeed running the function that is named that as you can see below.

xmlHTTP.onreadystatechange = handleServerResponse; 

有一个叫handleServerResponse()函数,这条线实际运行。我可以将它张贴,但我认为这是无关紧要的。这只是一个正常的功能函数handleServerResponse()。任何的解释是大大AP preciated! 谢谢! 〜Carpetfizz

There's a function called "handleServerResponse()", that this line actually runs. I can post it, but I think it's irrelevant. It's just a normal function function handleServerResponse(). Any explanation would be greatly appreciated! Thanks! ~Carpetfizz

编辑:添加()到该行的末尾,造成错误,以及改变它。

Adding () to the end of that line, creates errors, as well as changing it.

推荐答案

他们在做什么存在的引用应用于功能没有的呼叫的吧。

What they're doing there is referring to the function without calling it.

var x = foo;   // Assign the function foo to x
var y = foo(); // Call foo and assign its *return value* to y

在JavaScript中,函数是对象。合适的对象。所以,你可以通过引用它们。

In JavaScript, functions are objects. Proper objects. And so you can pass references to them around.

在特定情况下,他们正在做的是建立 handleServerResponse 作为XHR对象使用的就绪状态发生变化时的回调。 XHR对象将做Ajax请求的过程中调用该函数。

In that specific case, what they're doing is setting up handleServerResponse as the callback that the XHR object uses when the ready state changes. The XHR object will call that function during the course of doing the ajax request.

更多的例子:

// Declare a function
function foo() {
    console.log("Hi there");
}

// Call it
foo();        // Shows "Hi there" in the console

// Assign that function to a varible
var x = foo;

// Call it again
x();          // Shows "Hi there" in the console

// Declare another function
function bar(arg) {
    arg();
}

// Pass `foo` into `bar` as an argument
bar(foo);     // Shows "Hi there" in the console, because `bar`
              // calls `arg`, which is `foo`

这是继自然的事实,函数是对象,但它是值得呼唤特别是有X 和富在上面;他们都只是变量指向相同的功能。除了它们指向相同功能的事实,它们不以任何方式连接,并且改变酮(以点另一个功能,例如)对其他没有影响。例如:

It follows on naturally from the fact that functions are objects, but it's worth calling out specifically that there's no magic link between x and foo in the above; they're both just variables that point to the same function. Other than the fact they point to the same function, they're not linked in any way, and changing one (to point at another function, for instance) has no effect on the other. Example:

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`)