javascript 在 nodejs 上访问 function.caller
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25779491/
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
Accessing the function.caller on nodejs
提问by Michael
I have a task that depends upon function.caller to sanity check that a caller is authorized.
我有一个任务依赖于 function.caller 来检查调用者是否被授权。
According to this url, caller is supported in all major browsers... and all of my unit tests pass:
根据这个 url,所有主要浏览器都支持调用者......我所有的单元测试都通过了:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller
However, nodejs rejects all attempts to access function.caller (reports it as null).
但是,nodejs 拒绝所有访问 function.caller 的尝试(将其报告为 null)。
I'm open to suggestions to make this work on nodejs... I'd hate to have this framework only work on browsers.
我愿意接受在 nodejs 上进行这项工作的建议......我不想让这个框架只在浏览器上工作。
Thanks!
谢谢!
回答by eff_it
The Caller-id npm package makes it as easy as pie : https://www.npmjs.com/package/caller-idfrom the docs :
Caller-id npm 包使它像馅饼一样简单:https: //www.npmjs.com/package/caller-id来自文档:
var callerId = require('caller-id');
// 1. Function calling another function
function foo() {
bar();
}
function bar() {
var caller = callerId.getData();
/*
caller = {
typeName: 'Object',
functionName: 'foo',
filePath: '/path/of/this/file.js',
lineNumber: 5,
topLevelFlag: true,
nativeFlag: false,
evalFlag: false
}
*/
}
回答by Tom B
I'm rather late to this party but I was also trying to use arguments.caller and after a lot of trial and error, it's possible if you use prototype.
我参加这个聚会比较晚,但我也尝试使用arguments.caller,经过多次反复试验,如果您使用原型,则有可能。
This does not work:
这不起作用:
class MyClass {
watchProperties() {
Object.defineProperty(this, 'myproperty', {
get: function f(value) {
},
set: function f(value) {
console.log(arguments.caller.name);
}
});
}
}
var foo = new MyClass;
foo.watchProperties();
foo.myproperty = 'bar';
But by using prototype instead it does:
但是通过使用原型代替它:
class MyClass {
类我的类{
}
}
MyClass.prototype.watchProperties = function() {
Object.defineProperty(this, 'myproperty', {
get: function f(value) {
},
set: function f(value) {
console.log(arguments.caller.name);
}
});
};
var foo = new MyClass;
foo.watchProperties();
foo.myproperty = 'bar';
This is functionally identical. I have no idea why one is prevented by node and the other isn't but this is a working workaround in node 6.0.
这在功能上是相同的。我不知道为什么一个被节点阻止而另一个不是,但这是节点 6.0 中的一个有效解决方法。
回答by Michael
Because function.caller DOES work in nodejs, but fails when combined with Object.defineProperty getters/setters, I'm going to consider this a bug in nodejs, rather than a choice by nodejs not to support function.caller.
因为 function.caller 确实在 nodejs 中工作,但是当与 Object.defineProperty getter/setter 结合使用时会失败,我将认为这是 nodejs 中的错误,而不是 nodejs 选择不支持 function.caller。
回答by mscdex
You can use arguments.callee.caller
to get a reference to the caller. However I believe it's (still) deprecated (although its eventual removal could cause a problem for some cases, see herefor that discussion) and you miss out on some compiler/interpreter optimizations too.
您可以使用arguments.callee.caller
来获取对调用方的引用。但是我相信它(仍然)已被弃用(尽管它的最终删除可能会在某些情况下导致问题,请参见此处的讨论)并且您也错过了一些编译器/解释器优化。
Example:
例子:
function foo() {
bar();
}
function bar() {
console.log(arguments.callee.caller.name);
}
foo();
// outputs: foo