如何判断一个对象是否包含指向特定引用的属性?
给定这两个对象:
var object1 = {
a: function() {},
b: function() {},
c: function() {}
};
var object2 = {
d: function() {},
e: function() {},
f: function() {}
};
这里我们有两个对象,每个对象包含 3 个属性,它们是函数对象(或者,准确地说,是对函数对象的引用)。
假设 f
是对这 6 个函数对象之一的引用。 (它的声明如下: var f = object2.e;
或 var f = object1.c;
。)
我如何确定引用是否 f
是 object1
的 3 个引用/属性之一吗?
Given these two objects:
var object1 = {
a: function() {},
b: function() {},
c: function() {}
};
var object2 = {
d: function() {},
e: function() {},
f: function() {}
};
Here we have two objects each containing 3 properties which are function objects (or, to be precise, references to function objects).
Let's say that f
is a reference to one of those 6 function objects. (It was declared like so: var f = object2.e;
or var f = object1.c;
.)
How can I determine whether or not the reference f
is among the 3 references/properties of object1
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您唯一能做的就是迭代对象的属性:
f
并不是真正指向其中一个对象的属性。它更像是,f
和属性,都指向相同的值/对象。The only thing you can do is to iterate over the object's properties:
f
is not really pointing to a property of one of the objects. It is more like both,f
and the property, point to the same value/object.所以这是我当前的解决方案(基于@Felix的答案):
然后:然后
:
现场演示: http://jsfiddle.net/W3Lub/
你觉得怎么样?我很难相信没有浏览器或库提供此功能?!
So this is my current solution (based on @Felix's answer):
And then:
and:
Live demo: http://jsfiddle.net/W3Lub/
What do you think? I find it hard to believe that no browser or library offers this feature?!