繁体   English   中英

替换新函数内的arguments.callee

[英]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在构造函数内工作,无论严格模式。

有关该主题的其他链接:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM