gpt4 book ai didi

javascript - arguments.callee 的使用没有好的选择吗?

转载 作者:行者123 更新时间:2023-11-29 22:00:59 24 4
gpt4 key购买 nike

关于 arguments.callee 有很多谬误,我试图了解是否存在确实无法用可行的 ES5 严格模式替换的用例 备选方案。

MDN arguments.callee他们指向的文档 a use of arguments.callee with no good alternative使用下面的代码示例:

function createPerson (sIdentity) {
var oPerson = new Function("alert(arguments.callee.identity);");
oPerson.identity = sIdentity;
return oPerson;
}

var john = createPerson("John Smith");

john();

他们包括链接一个bug表明在某些情况下,argument.callee 不能被符合 ES5 严格模式 的代码替换。

但在理解中,他们用作示例的代码可以替换为以下严格模式替代方案:

"use strict";

function createPerson(sIdentity) {
var oPerson = function () {
alert(oPerson.identity);
};

oPerson.identity = sIdentity;
return oPerson;
}

var john = createPerson("John Smith");

john();

既然如此,真的存在一些无法替换 arguments.callee 的算法吗?

赏金

为了赢得赏金,我希望答案包含 arguments.callee 的用法,其中使用其他解决方案会更加晦涩或不可能。

在 MDN 示例中,我编写的替代版本不会更改该段代码的用法。

最佳答案

这是一个用例:为内联事件处理程序(可能来自生成/模板代码)保留一个变量,而不会污染全局命名空间或 DOM 或冒其他名称冲突的风险:

<button onclick="var f=arguments.callee;alert(f.i=(f.i||0)+1)">CLICK</button>

Demonstration

现在,假设这不是合法用途。当然没有真正合法的,否则 arguments.callee 就不会被砍掉。

关于javascript - arguments.callee 的使用没有好的选择吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23784392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com