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;
};
This method returns 0, and iterating over those properties using:
for (foo in Model_value)
{
//...
}
does not work.
How would I iterate over those PK-34, PK-35 properties?
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.
Example:
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));
Live copy
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).
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
.
In the third case, bar
has both two inherited properties and one property of its own, so size
is 3
and sizeInherited
is 2
.
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;
};
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;
};
Example:
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")
Live copy
Note that the functions we've added to Model_value
are assigned to properties, and so they show up in the totals.
精彩评论