javascript:索引期间对对象调用哪些函数?

发布于 2024-10-10 08:11:59 字数 456 浏览 0 评论 0原文

这可能是一个措辞不好的问题,但考虑一下这个片段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2024-10-17 08:11:59

听起来好像您正在寻找像 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.

兰花执着 2024-10-17 08:11:59

第一部分的答案是创建您自己的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文