Javascript 'caller' 和 'arguments' 是受限制的函数属性,无法在此上下文中访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31921189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'caller' and 'arguments' are restricted function properties and cannot be accessed in this context
提问by Brian M. Hunt
I am trying to create a simple debugging function that simply shows the caller of a function, like this:
我正在尝试创建一个简单的调试函数,它只显示函数的调用者,如下所示:
function xe() {
console.log(xe.caller().name)
}
With this I would just be able to add xe()
to a function and it will log the calls to the function– just a short, simple addition to help with debugging. Debugging sugar, so to speak.
有了这个,我就可以添加xe()
到一个函数中,它会记录对该函数的调用——只是一个简短的、简单的添加来帮助调试。调试糖,可以这么说。
Unfortunately I get the error from the subject-line:
不幸的是,我从主题行中得到了错误:
TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
TypeError: 'caller' 和 'arguments' 是受限制的函数属性,无法在此上下文中访问。
I am using Babel/ES6, which injects "use strict"
at the top of every module. This maybe the cause, but searching has yielded limited information on why the error is raised, and I would like to understand it better.
我正在使用 Babel/ES6,它"use strict"
在每个模块的顶部注入。这可能是原因,但搜索产生的关于错误发生原因的信息有限,我想更好地理解它。
If strict mode is the problem I would prefer not to disable strict mode for the entire project– just for the module/function.
如果严格模式是问题所在,我不希望为整个项目禁用严格模式——仅针对模块/函数。
回答by Kit Sunde
It is the cause. From MDN:
这是原因。来自 MDN:
in strict mode it's no longer possible to "walk" the JavaScript stack via commonly-implemented extensions to ECMAScript. In normal code with these extensions, when a function fun is in the middle of being called, fun.caller is the function that most recently called fun, and fun.arguments are the arguments for that invocation of fun. Both extensions are problematic for "secure" JavaScript because they allow "secured" code to access "privileged" functions and their (potentially unsecured) arguments. If fun is in strict mode, both fun.caller and fun.arguments are non-deletable properties which throw when set or retrieved:
在严格模式下,不再可能通过 ECMAScript 的常用扩展来“遍历”JavaScript 堆栈。在带有这些扩展的普通代码中,当函数 fun 正在被调用时, fun.caller 是最近调用 fun 的函数,而 fun.arguments 是调用 fun 的参数。这两个扩展对于“安全”JavaScript 来说都是有问题的,因为它们允许“安全”代码访问“特权”函数及其(可能不安全的)参数。如果 fun 处于严格模式,则 fun.caller 和 fun.arguments 都是不可删除的属性,它们在设置或检索时会抛出:
If you're doing ES6, you can't in the general case disable strict mode. It's implicit during certain conditionssuch as when in an ES6 module.
如果您正在使用 ES6,在一般情况下您不能禁用严格模式。它在某些条件下是隐式的,例如在 ES6 模块中。
If you're just debugging, I'd suggest just using a break point in a debugger and checking the stack frame, but I'm sure you know that already.
如果您只是在调试,我建议您只在调试器中使用断点并检查堆栈帧,但我相信您已经知道了。
If you're just outputting debugging information you could also, I suppose just read the stack of an Error object:
如果您只是输出调试信息,您也可以,我想只需读取 Error 对象的堆栈:
console.log(new Error().stack);
You can globaly disable(but I realize this isn't what you want) use strict
with babel Using either:
您可以使用 babel全局禁用(但我意识到这不是您想要的)use strict
使用:
require("6to5").transform("code", { blacklist: ["useStrict"] });
or
或者
$ 6to5 --blacklist useStrict
If you must strip it out on a module level, I suspect you'll have to do it yourself. Basic string replace perhaps?
如果您必须在模块级别剥离它,我怀疑您必须自己完成。可能是基本字符串替换?
Additionally, as has been pointed out in ES5. It should be xe.caller.name
and not xe.caller().name
or you would re-invoke the function.
此外,正如在 ES5 中指出的那样。它应该是xe.caller.name
而不是,xe.caller().name
否则您将重新调用该函数。
回答by Laxmikant Dange
As per thisdocumentation. The Function.caller()
property returns the function that invoked the specified function. Simply you will get whole caller function when you use xe.caller
. Again you are executing caller function. Here you are doing recursionand that is the reason it is not allowing in strict mode.
根据本文档。该Function.caller()
属性返回调用指定函数的函数。简单地说,当您使用xe.caller
. 您再次执行调用函数。在这里你正在做递归,这就是它在严格模式下不允许的原因。
You can execute your sample function in browser console. you will get Maximum call stack size exceeded
error.
您可以在浏览器控制台中执行示例函数。你会得到Maximum call stack size exceeded
错误。