javascript:如何在函数本身中引用匿名函数?
如果“use strict”中不允许使用arguments.callee,并且我们不能这样做,
var f = function g() {
//g
}
因为在IE中这不起作用(或者“奇怪”地工作)http://kangax.github.com/nfe/#jscript-bugs,那么我们还有什么其他选项来引用匿名函数?函数本身?
if arguments.callee is not allowed in "use strict", and we can't do
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是Y 组合器 的用途。
这是 James Coglan 撰写的一篇关于在 JavaScript 中导出 Y 组合器的文章。
That's precisely what the Y combinator is for.
Here's an article by James Coglan about deriving the Y combinator in JavaScript.
不要使用命名函数表达式。只需以正常方式声明并初始化它即可。
ES5 strict 的唯一可行替代方案是使用问题中的代码,并处理 IE 蹩脚的 NFE 实现。但是:你真的希望一个 NFE 错误如此严重的浏览器(咳咳,IE)很快就能实现
“use strict”
吗?Don't use a named function expression. Just declare and initialize it the normal way.
The only viable alternative with ES5 strict is to use the code in your question, and deal with IE's crappy NFE implementation. But: do you really expect a browser that gets NFEs so horribly wrong (ahem, IE) to implement
"use strict"
anytime soon?这是一种相当复杂的方法,但它有效:
http://jsfiddle.net/4KKFN/4/< /a>
Here's a rather convoluted way to do it, but it works:
http://jsfiddle.net/4KKFN/4/