JavaScript自调用函数

发布于 2024-10-24 12:51:08 字数 657 浏览 5 评论 0原文

我从 Stoyan Stefanov 所著的《面向对象 JavaScript》一书中的第 79 页得到了这个示例。我真的不知道该怎么做,第一次运行这个程序(按回车键)它返回“未定义”。之后,按照作者的指示,我将其命名为 a(); 并收到警报“Worky worky”

我的问题是

a) 我是否正确执行了第一步?即我是否应该仅通过点击“输入/返回”来运行自调用程序?

b)如果我是正确的,只是点击“输入/返回”来运行程序,为什么它给出的结果是“未定义”。作者说,这个程序(在第一次运行时)拒绝对函数actualWork()的引用?如果它返回一个引用,为什么它被认为是未定义的?它在某种程度上重要吗?

请注意,我尝试在 jsfiddle.net 中输入代码,然后点击运行,但没有发生任何事情,但是当我第一次在控制台中运行它时,我得到了“未定义”,然后当我执行 a(); 时出现警报;

var a = function() {
    function someSetup(){
        var setup = 'done';
    }
    function actualWork(){
        alert('Worky-worky');
    }
    someSetup();
    return actualWork;
}();

I got this example from page 79 of a book called Object Oriented JavaScript by Stoyan Stefanov. Not really knowing what to do, the first time I ran this program (by hitting enter) it returned 'undefined'. After that, following the author's instructions, I called it a(); and got the alert 'Worky worky'

My questions are

a) Did I do the first step correctly? i.e. am I supposed to run a self-invoking program by merely hitting "enter/return"?

b) if I was correct to just hit "enter/return" to run the program, why did it give a result of "undefined." This program, says the author, refuns a reference (on its first run-through) to the function actualWork() ? if it returns a reference, why is that considered undefined? Is it important somehow?

Note that I tried to enter the code in jsfiddle.net and then hit run and nothing happened, but that I got "undefined" when I ran it the first time in the console and then the alert afterwards when I did a();

var a = function() {
    function someSetup(){
        var setup = 'done';
    }
    function actualWork(){
        alert('Worky-worky');
    }
    someSetup();
    return actualWork;
}();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深居我梦 2024-10-31 12:51:08

此代码相当于:

var f = function() {
    function someSetup(){
        var setup = 'done';
    }
    function actualWork(){
        alert('Worky-worky');
    }
    someSetup();
    return actualWork;
};

var a = f();

因此:您正在创建变量 f 并为其分配一个函数。然后将调用 f() 的结果分配给 a,它恰好也是一个函数(这是在原始代码中隐式完成的)。最后,您可以运行a(),它运行由f()返回的函数。

This code is equivalent to:

var f = function() {
    function someSetup(){
        var setup = 'done';
    }
    function actualWork(){
        alert('Worky-worky');
    }
    someSetup();
    return actualWork;
};

var a = f();

So: You are creating variable f and assigning a function to it. Then You assign the result of invoking f() to a, which happens to be a function as well (this is done implicitly in the original code). And at the very end you can run a(), which runs the the function returned by f().

晨敛清荷 2024-10-31 12:51:08

“自调用”意味着该代码在被编译器执行后立即生效,即使所有代码都在函数内部。发生这种情况是因为该函数被立即调用(最后一行中的那些 () )。

如果您只是编写,这也是会发生的情况。

function someSetup(){
    var setup = 'done';
}
function actualWork(){
    alert('Worky-worky');
}
someSetup();
return actualWork;

这里的区别在于,“自调用程序”(恕我直言,这是对其含义的错误描述)允许您在不使用名称 someSetup< 的情况下执行此操作/code> 和 actualWork 对调用代码可见——这通常是可取的。

解释完后,让我们回答您的问题:

  1. 代码片段本身不会返回任何内容(即使它分配给a返回的内容一个函数);因此,您的 JS IDE 报告其返回值为 undefined
  2. 对函数 actualWork 的引用可以正常返回(它是分配给 a 的值);当您使用 a() 成功调用它时,您自己就会看到这一点。

"Self-invoking" means that this code has an immediate effect immediately after it is executed by the compiler, even though all the code is inside a function. This happens because the function is immediately invoked (those () in the last line).

This is also what would have happened if you wrote just

function someSetup(){
    var setup = 'done';
}
function actualWork(){
    alert('Worky-worky');
}
someSetup();
return actualWork;

The difference here is that the "self-invoking program" (which is a bad description for what it is, IMHO) allows you to do this without making the names someSetup and actualWork visible to the calling code -- this is generally desirable.

With that explained, let's answer your questions:

  1. The code snippet does not return anything itself (even if it assigns to a something that is returned by a function); therefore your JS IDE reports that its return value is undefined.
  2. The reference to function actualWork is returned fine (it's the value assigned to a); you saw that yourself when you successfully called it with a().
眼泪淡了忧伤 2024-10-31 12:51:08

a) 取决于你的解释器的工作方式,是的,只需运行脚本(按回车键)即可定义函数 a();

b) 它给出“未定义”,因为程序本身不返回任何内容,但函数 a();做。
上面引用的代码代表一个函数 a();当调用时,应该执行以下操作:

  1. 定义 2 个其他(临时)函数,
  2. 运行其中一个(someSetup),
  3. 返回另一个。

所以我会通过以下方式使用该程序:

  1. 运行来定义 a();
  2. 调用 var x=a();
  3. (可选)检查 setup=='done' 是否
  4. 调用 x(); (由a()) 应显示警报。

编辑
抱歉,我没有看到}();最后,所以它不是100%正确的。这 ();最后是 - 正如 Jon 和 Tomasz 所说 - 缩写形式“将函数的返回值作为新函数并立即运行它”。

a) depending on how your interpreter works, yes, simply running the script (by pressing return) defines function a();

b) it gave "undefined" because the program itself doesn't return anything, but the function a(); does.
the code you quoted above represents a function a(); which, when invoked, should do the following:

  1. define 2 other (temporary) functions
  2. run one of them (someSetup)
  3. return the other one.

so i would use the program by:

  1. running to define a();
  2. calling var x=a();
  3. (optional) checking if setup=='done'
  4. calling x(); (which was returned by a()) should show the alert.

EDIT
sorry, i didn't see the }(); at the end, so it's not 100% correct. the (); at the end is - as Jon and Tomasz both said - short form for "take the function's return value as a new function and run it immediately".

场罚期间 2024-10-31 12:51:08

undefinded 从语句 var a = ... 返回,这是可以的。引用由赋值的右侧返回。

undefinded is returned from statements var a = ... that is OK. Reference is returned by the right side of assigment.

不弃不离 2024-10-31 12:51:08

匿名函数会立即被调用,并返回对其中函数 actualWork 的引用。所以a包含这个引用并且可以被它自己调用。所以 a(); 应该给你警报。

The anonumous function is invoked immediately and returns a reference to the function actualWork within it. So a contains this reference and can be invoked by itself. So a(); should give you the alert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文