function main()
{
Hello();
}
function Hello()
{
// How do you find out the caller function is 'main'?
}
Run Code Online (Sandbox Code Playgroud)
有没有办法找出调用堆栈?
我正在尝试创建一个简单的调试函数,只显示函数的调用者,如下所示:
function xe() {
console.log(xe.caller().name)
}
Run Code Online (Sandbox Code Playgroud)
有了这个,我只能添加xe()一个函数,它将记录对函数的调用 - 只是一个简单的简单添加,以帮助调试.调试糖,可以这么说.
不幸的是我从主题行得到错误:
TypeError:'caller'和'arguments'是受限制的函数属性,在此上下文中无法访问.
我正在使用Babel/ES6,它注入"use strict"每个模块的顶部.这可能是原因,但搜索已经产生了有关错误引发原因的有限信息,我想更好地理解它.
如果严格模式是问题,我宁愿不为整个项目禁用严格模式 - 仅用于模块/功能.
在框架中,我正在开发,我已经构建了机制,允许定义私有和受保护的属性和方法.
我在ES5规范中发现的唯一能力是使用arguments.callee,
如下所示:
descriptor.method = function () {
if (__callerIsProptected(arguments.callee.caller.caller, cls))
return value.apply(this, __defaults(_.values(arguments), defaults));
throw 'Attempt to call ' + access + ' method "' + cls._name + '::' + name + '"';
};
Run Code Online (Sandbox Code Playgroud)
至于在严格模式下调用arguments.callee和arguments.caller引起异常抛出是否有任何方便的替代方法呢?
更新 - 添加整个被调用的功能代码
function __descriptor(cls, type, name, descriptor, access) {
//protected private non-function descriptor.value is replaced by get/set pair
if (access !== 'public' && type == 'property') {
delete descriptor.value;
delete descriptor.writable;
_.isFunction(descriptor.get) || (descriptor.get = function () {
return this.__get(name);
});
_.isFunction(descriptor.set) …Run Code Online (Sandbox Code Playgroud)