SpiderMonkey:我如何从c中获取当前JSObject的名称?
有人问如何获取来自 c 的 JSObject 属性。这对我有一点帮助。
但是,有谁知道如何从 c 中获取对象的当前 JavaScript 名称?
示例:
var foo={prop:'bar'};
然后在 jsapi.cpp 中的某个地方:
JS_somemethod(JSContext *cx, JSObject *obj){
//how do i get the name 'foo' (or the current alias) here if *obj points to the foo (or whatever alias name) object?
}
感谢提示和答案!
someone asked how to get the value of a JSObject property from c. That helped me a bit.
But, does anyone know how to get the current JavaScript name of an object from c?
example:
var foo={prop:'bar'};
then somewhere for example in jsapi.cpp:
JS_somemethod(JSContext *cx, JSObject *obj){
//how do i get the name 'foo' (or the current alias) here if *obj points to the foo (or whatever alias name) object?
}
Thx for hints and answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,为了记录您的评论中的问题澄清,我将在这里重复一遍:
预先说明一下,这通常是相当棘手的。有几个选项可供您使用,所有这些选项都有些困难:
调试器对象绝对是我在这里列出的选项中最喜欢的选项。如果您采用这种方法并最终陷入困境,请随意访问 irc.mozilla.org 中的 #jsapi 并尝试获取 jorendorff 或 jimb,他们 a) 非常棒,b) 调试器对象的创建者。
Okay, just to document the question clarification from your comment I'll repeat it here:
Just to say it up front, this is pretty tricky in general. There are a few options available to you, all of which are somewhat difficult:
The debugger object is definitely my favorite option of those I've listed here. If you go with that approach and end up getting stuck feel free to visit #jsapi in irc.mozilla.org and try to get a hold of jorendorff or jimb, they're a) awesome and b) the creators of the debugger object.
在不仔细研究 API 的情况下,我想说的是,虽然在一些非常基本的情况下可能可以这样做,但您不想这样做。这就是为什么:
在 Javascript 中,与大多数语言一样,变量指向值。变量不是值本身。该对象与名称 foo 没有任何内在联系。
例如,您可以执行
所有这些
foo
现在都是完全相同的 foo;你想找回哪一件?没有对此进行 API 调用,因为它太含糊而且不是很有用。但是,如果您确实想找到一个保存值的全局变量,您可以尝试迭代全局对象上的键并测试它们的值。
您必须将其转换为 API 调用,或者将其放入函数中并通过 API 调用该函数。
更简单直接的解决方案是弄清楚您稍后想要对 foo 做什么,并传入一个回调函数来执行此操作,例如使用 JS_CallFunction。
tl;dr:上一段。
Without looking too closely at the API, I'm going to say that while it might be possible to do this in some very basic cases, you don't want to. This is why:
In Javascript, as in most languages, variables point to values. Variables are not the values themselves. The object is not in any way inherently related to the name foo.
For instance, you can do
All of those
foo
s are now the exact same foo; which one do you want back? There's no API call for that because it's too ambiguous and not terribly useful.However, if you really want to find one of the global variables holding a value, you can try iterating over the keys on the global object and testing them for the value.
You'd have to translate this into your API calls or else put it in a function and call that through the API.
The more simple and straightforward solution would be to figure out what you want to do with foo later on, and pass in a callback function that will do that, e.g. with JS_CallFunction.
tl;dr: Previous paragraph.