问题描述
我有一个变量,当打印到控制台时,它看起来像这样:
I have a variable, which, when printed to console, looks like this:
Object { PK-34={...}, PK-35={...}}
我正在添加尺码此变量的方法:
I'm adding a size method to this variable:
Model_value.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
此方法返回0,并使用以下内容迭代这些属性:
This method returns 0, and iterating over those properties using:
for (foo in Model_value)
{
//...
}
不起作用。
我如何迭代那些PK-34,PK- 35个属性?
How would I iterate over those PK-34, PK-35 properties?
推荐答案
如果 size
最终为零,显然是有问题的对象是从其原型继承这些属性。在这种情况下,这意味着您不希望 hasOwnProperty
检查。
If size
ends up being zero, apparently the object in question is inheriting those properties from its prototype. In that case, it means you don't want the hasOwnProperty
check.
示例:
var Model_value = {};
Model_value.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
Model_value.sizeInherited = function(obj) {
var size = 0, key;
for (key in obj) {
size++;
}
return size;
};
function Foo() {
}
Foo.prototype["PK-34"] = {
name: "I'm PK-34"
};
Foo.prototype["PK-35"] = {
name: "I'm PK-35"
};
display("Model_value.size(new Foo()) = " + Model_value.size(new Foo()));
display("Model_value.sizeInherited(new Foo()) = " + Model_value.sizeInherited(new Foo()));
var f = {
"PK-34": {name: "I'm PK-34"},
"PK-35": {name: "I'm PK-35"}
};
display("Model_value.size(f) = " + Model_value.size(f));
display("Model_value.sizeInherited(f) = " + Model_value.sizeInherited(f));
var bar = new Foo();
bar["PK-36"] = {name: "I'm PK-36"};
display("Model_value.size(bar) = " + Model_value.size(bar));
display("Model_value.sizeInherited(bar) = " + Model_value.sizeInherited(bar));
在第一种情况下( new Foo()
),新对象由 Foo
创建,没有自己的属性(只有继承的属性),因此 size
最终为 0
而 sizeInherited
是 2
(因为它从其原型继承了两个属性) 。
In the first case (new Foo()
), the new object created by Foo
has no properties of its own (only inherited ones) and so size
ends up being 0
whereas sizeInherited
is 2
(because it inherits two properties from its prototype).
在第二种情况下,因为 f
的拥有 PK-34
和 PK-35
属性,其大小
是 2
。它没有继承原型中的属性,因此 sizeInherited
也是 2
。
In the second case, because f
has its own PK-34
and PK-35
properties, its size
is 2
. It inherits no properties from its prototype, and so sizeInherited
is also 2
.
在第三种情况下, bar
有两个继承的属性和一个属性,所以 size
是 3
和 sizeInherited
是 2
。
In the third case, bar
has both two inherited properties and one property of its own, so size
is 3
and sizeInherited
is 2
.
更新:编辑似乎有点改变了问题。如果你试图遍历 <$ strong> <$ strong> Model_value
中的属性,那么你不想接受一个参数,你想要使用此
:
Update: The edit seems to change the question a bit. If you're trying to iterate over the properties in Model_value
, then you don't want to accept an argument, you want to use this
:
要查找它拥有多少拥有的属性:
To find how many own properties it has:
Model_value.size = function() {
var size = 0, key;
for (key in this) {
if (this.hasOwnProperty(key)) size++;
}
return size;
};
要查找它有多少属性 total (包括继承的属性):
To find how many properties total it has (including inherited ones):
Model_value.size = function() {
var size = 0, key;
for (key in this) {
size++;
}
return size;
};
示例:
function Model() {
}
Model.prototype["PK-34"] = {
name: "I'm PK-34"
};
Model.prototype["PK-35"] = {
name: "I'm PK-35"
};
var Model_value = new Model();
Model_value.size = function() {
var size = 0, key;
for (key in this) {
if (this.hasOwnProperty(key)) size++;
}
return size;
};
Model_value.sizeInherited = function() {
var size = 0, key;
for (key in this) {
size++;
}
return size;
};
display("Model_value.size() = " + Model_value.size());
// ^-- returns 2 ("size" and "sizeInherited")
display("Model_value.sizeInherited() = " + Model_value.sizeInherited());
// ^-- returns 4 ("PK-34", "PK-35", "size", and "sizeInherited")
请注意,我们添加到 Model_value
的函数已分配给属性,因此它们会显示在总计中。
Note that the functions we've added to Model_value
are assigned to properties, and so they show up in the totals.
这篇关于Javascript对象:迭代属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!