'caller'和'arguments'是限制的函数属性,在这种情况下无法访问 [英] 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context

查看:2898
本文介绍了'caller'和'arguments'是限制的函数属性,在这种情况下无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的调试功能,只需显示一个函数的调用者,如下所示:

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)
}

这样我就可以将 xe()添加到一个函数中,它会将调用记录到函数中 - 只需一个简短的简单的附加功能即可调试。

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.

不幸的是,我从主题行得到错误:

Unfortunately I get the error from the subject-line:


TypeError:'caller'和'arguments'是受限的功能属性,在这种情况下无法访问。

TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.

I正在使用Babel / ES6,它在每个模块的顶部注入使用脚本。这个可能是原因,但搜索已经产生了有关为什么错误提出的有限信息,我想更好地了解它。

I am using Babel/ES6, which injects "use script" at the top of every module. This may be the cause, but searching has yielded limited information on why the error is raised, and I would like to understand it better.

如果严格模式是问题,我宁愿不为整个项目禁用严格的模式 - 只是为了模块/功能。

If strict mode is the problem I would prefer not to disable strict mode for the entire project– just for the module/function.

推荐答案

从MDN :


在严格模式下,不再可能通过ECMAScript的常用扩展走JavaScript堆栈。在这些扩展的正常代码中,当函数fun被调用时,fun.caller是最近称为fun的函数,fun.arguments是有趣的参数。这两个扩展对于安全JavaScript是有问题的,因为它们允许安全代码访问特权函数及其(潜在的不安全的)参数。如果乐趣处于严格模式,fun.caller和fun.arguments都是不可删除的属性,在设置或检索时抛出:

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 is 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:

如果您正在做ES6,则在一般情况下,您不能禁用严格模式。这在某些条件(例如在ES6模块中)是隐含的。

If you're doing ES6, you can't in the general case disable strict mode. It's implicit during certain conditions such as when in an ES6 module.

如果你只是调试,我建议在调试器中使用一个断点并检查堆栈框架,但我确信你已经知道了。

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.

如果你只是输出调试信息,我也可以,只是读一个Error对象的堆栈:

If you're just outputting debugging information you could also, I suppose just read the stack of an Error object:

console.log(new Error().stack);

您可以 globaly disable (但我意识到这不是你想要的)使用strict 与babel使用(code,{blacklist:[useStrict]});

You can globaly disable (but I realize this isn't what you want) use strict with babel Using either:

require("6to5").transform("code", { blacklist: ["useStrict"] });

$ 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?

另外,如ES5所指出的那样。它应该是 xe.caller.name 而不是 xe.caller()。name 或者你将重新调用功能。

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.

这篇关于'caller'和'arguments'是限制的函数属性,在这种情况下无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆