简体   繁体   English

声明后执行命名函数

[英]Executing a named function after declaring it

I know that this can be done for anonymous functions 我知道这可以用于匿名函数

(function toBeExecutedImmediately() {
    // Code
}());

I have a function that I wish to use in other places, but should also be executed immediately. 我有一个我希望在其他地方使用的功能,但也应该立即执行。 Is it possible to do it with one statement instead of the following ? 是否可以使用一个语句而不是以下语句来执行此操作 The return value isn't needed. 不需要返回值。

function toBeExecutedImmediately() {
    // Code
};

toBeExecutedImmediately();

Is it possible to do it with one statement instead of the following? 是否可以使用一个语句而不是以下语句来执行此操作?

No. As you've found, the named function expression (your first example) doesn't add the name to the containing scope (except on broken engines — I'm looking at you, IE8 and earlier) , so you can't call the function elsewhere without doing something to make that possible, and of course with the function declaration (your second example), you have to give the name twice. 没有。正如您所发现的那样,命名函数表达式(您的第一个示例)不会将名称添加到包含范围(除了损坏的引擎 - 我正在看着您,IE8及更早版本) ,所以您不能在没有做任何事情的情况下调用其他地方的函数,当然使用函数声明 (你的第二个例子),你必须给出两次名称。

Your best bet is probably to just go ahead and do the declaration and the separate call (your second example). 你最好的选择可能是继续做声明和单独的电话(你的第二个例子)。

We could probably come up with something convoluted that would technically meet the definition of "a single statement," but it's unlikely to be shorter. 我们可能会想出一些在技术上符合“单一陈述”定义的复杂问题,但它不可能更短。

If brevity is your goal, this is two statements, but brief: 如果简洁是你的目标,这是两个陈述,但简短:

var f = function() {
    // Code
}, toBeExecutedImmediately = f;
f();

But while relatively brief, I'd say clarity suffers. 但是虽然相对简短,但我认为清晰度会受到影响。 :-) :-)

Of course, anything is possible if you add a helper function. 当然,如果添加辅助函数,任何事情都是可能的。 For instance: 例如:

// The helper
function defineAndExecute(f) {
    f();
    return f;
}

// using it
var toBeExecutedImmediately = defineAndExecute(function() {
    // Do work here
});

The function is technically anonymous, but you can call it via the var . 该函数在技术上是匿名的,但您可以通过var调用它。 (You could use a named function expression here too, but of course that would defeat the goal — not repeating the name — as I understand it.) (你也可以在这里使用一个命名的函数表达式,但当然这会打败目标 - 不重复这个名字 - 正如我所理解的那样。)


Side note: Your first example is not "an anonymous function". 旁注:你的第一个例子不是“匿名函数”。 The function has a proper name, it's a named function expression. 该函数具有正确的名称,它是一个命名函数表达式。 It's just that with named function expressions, unlike function declarations, that name isn't added to the containing scope. 只是使用命名函数表达式,与函数声明不同,该名称不会添加到包含范围。

You could use a namespace : 您可以使用命名空间:

// alerts "say yes"
(window.say = function (s) { alert('say ' + s); })('yes');
// alerts "say something"
say('something');

As a reply to the comments, here is how to make the function local : 作为对评论的回复,这里是如何使函数本地:

(function(){
    var dontSay;
    // alerts "don't say no"
    (dontSay = function (s) { alert('don\'t say ' + s); })('no'); 
    // alerts "don't say anything"
    dontSay('anything');
})();

@JoeSimmons It's not a revolution indeed, but the question is also not really essential ;D @JoeSimmons这确实不是一场革命,但这个问题也不是必不可少的; D

You can write sort of "function builder" like this: 您可以像这样编写“函数构建器”:

function DeclareAndExecute(name, func) {
    window[name] = func;
    func();
}

Usage: 用法:

DeclareAndExecute("toBeExecutedImmediately", function() {
    //code here
});

Live test case . 现场测试案例

This will add the function to the global window scope using the given name and insta-execute it. 这将使用给定名称将函数添加到全局window范围并执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM