javascript:索引期间对对象调用哪些函数?
这可能是一个措辞不好的问题,但考虑一下这个片段:
var foo = { bar : 1 };
var bar = foo['bar'];
索引到 foo 时实际上调用了哪些函数?同样,如果您有以下代码会怎样:
var foo = { bar : 1 };
for(var x in foo)
dosomething();
for 语句中实际上会调用 foo 上的哪些函数?假设我想更改给定对象的这两种情况的行为,我该怎么做?
更具体地说,我实际上正在做的是将 .net 对象传递到 MSHTML 文档中(上面的 foo 就是现实中的对象),该对象实现 IReflect 并在索引调用期间(如上面的场景 1)调用 GetProperties 和GetFields 已完成。我需要知道它正在寻找哪个属性,以便我可以实现它并让它调用我的对象。
This might be a poorly worded question but considering this snippet:
var foo = { bar : 1 };
var bar = foo['bar'];
What functions are actually called when indexing into foo? Similarly, what if you had the following code:
var foo = { bar : 1 };
for(var x in foo)
dosomething();
What functions on foo would actually be called in the for statement? Suppose I wanted to change the behavior for both of these scenarios for a given object, how would I do that?
To be more specific what I'm actually doing is passing a .net object into an MSHTML document (foo above would be the object in reality), the object implements IReflect and during an index call (like scenario 1 above) calls to GetProperties and GetFields are made. I need to know which property it's looking for so I can implement it and have it call my object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来好像您正在寻找像 PHP 中那样的神奇 getter 和 setter。
John Resig 写了一篇关于此的文章。但它并不适用于所有平台。
It sounds as if you are looking for magic getters and setters like in PHP.
John Resig wrote an article about that. It doesn't work on all platforms though.
第一部分的答案是创建您自己的 PropertyInfo 对象,该对象以集合中项目的索引命名并返回它们。然而,我仍然没有弄清楚如何与 for..in 兼容。
The answer is to the first part is to create your own PropertyInfo objects named after the indexes of items in your collection and return them . I still haven't figured out how to be compatible with for..in however.