[英]javascript: how to refer to an anonymous function within the function itself?
if arguments.callee is not allowed in "use strict", and we can't do 如果在“use strict”中不允许arguments.callee,我们就做不到
var f = function g() {
//g
}
because in IE that wouldn't work (or that would work "weirdly") http://kangax.github.com/nfe/#jscript-bugs , then what other options do we have to refer to the anonymous function within the function itself? 因为在IE中不起作用(或者说“奇怪”) http://kangax.github.com/nfe/#jscript-bugs ,那么我们还有哪些其他选项可以引用函数中的匿名函数本身?
Don't use a named function expression. 不要使用命名函数表达式。 Just declare and initialize it the normal way.
只需以正常方式声明并初始化它。
function f() {
f();
}
The only viable alternative with ES5 strict is to use the code in your question, and deal with IE's crappy NFE implementation. ES5严格的唯一可行替代方案是使用您问题中的代码,并处理IE糟糕的NFE实现。 But: do you really expect a browser that gets NFEs so horribly wrong (ahem, IE) to implement
"use strict"
anytime soon? 但是:你真的期望一个浏览器让NFE如此可怕的错误(咳咳,IE)很快就会实施
"use strict"
吗?
That's precisely what the Y combinator is for. 这正是 Y组合器的用途。
Here's an article by James Coglan about deriving the Y combinator in JavaScript. 这是James Coglan关于在JavaScript中派生Y组合子的文章。
Here's a rather convoluted way to do it, but it works: 这是一个相当复杂的方法,但它有效:
http://jsfiddle.net/4KKFN/4/ http://jsfiddle.net/4KKFN/4/
var f = function() {
function f() {
if (confirm('Keep going?')) {
this.apply(this);
}
}
f.apply(f);
}
f();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.