[英]Replace arguments.callee inside of new Function
一些字符串通过new Function(...)
调用转换为函数。
如何在不使用arguments.callee
情况下在字符串内部引用此函数?
var f = new Function('return arguments.callee.smth');
f.smth = 128;
f(); // 128
做这种事情的唯一方法是使用闭包。
例如:
var f = (function () {
var f = new Function('return this().smth').bind(function () { return f });
return f;
})();
f.smth = 128;
f();
或者:
var f = (function (g) {
return function f() {
return g.apply(f, arguments)
};
})(new Function('return this.smth'));
f.smth = 128;
f();
无论如何,似乎arguments.callee
在构造函数内工作,无论严格模式。
有关该主题的其他链接:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee
使用arguments.callee没有好的选择
但是,在如下情况下,没有
arguments.callee
替代方法,因此它的弃用可能是一个错误(参见错误725398 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.