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();
3 个解决方案
#1
4
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
. (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.)
该函数在技术上是匿名的,但您可以通过var调用它。 (你也可以在这里使用一个命名的函数表达式,但当然这会打败目标 - 不重复这个名字 - 正如我所理解的那样。)
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.
旁注:您的第一个示例不是“匿名函数”。该函数具有正确的名称,它是一个命名的函数表达式。只是使用命名函数表达式,与函数声明不同,该名称不会添加到包含范围。
#2
2
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
#3
1
You can write sort of "function builder" like this:
您可以像这样编写“函数构建器”:
function DeclareAndExecute(name, func) {
window[name] = func;
func();
}
Usage:
DeclareAndExecute("toBeExecutedImmediately", function() {
//code here
});
现场测试案例。
This will add the function to the global window
scope using the given name and insta-execute it.
这将使用给定名称将函数添加到全局窗口范围并执行它。
#1
4
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
. (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.)
该函数在技术上是匿名的,但您可以通过var调用它。 (你也可以在这里使用一个命名的函数表达式,但当然这会打败目标 - 不重复这个名字 - 正如我所理解的那样。)
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.
旁注:您的第一个示例不是“匿名函数”。该函数具有正确的名称,它是一个命名的函数表达式。只是使用命名函数表达式,与函数声明不同,该名称不会添加到包含范围。
#2
2
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
#3
1
You can write sort of "function builder" like this:
您可以像这样编写“函数构建器”:
function DeclareAndExecute(name, func) {
window[name] = func;
func();
}
Usage:
DeclareAndExecute("toBeExecutedImmediately", function() {
//code here
});
现场测试案例。
This will add the function to the global window
scope using the given name and insta-execute it.
这将使用给定名称将函数添加到全局窗口范围并执行它。