JavaScript 对象字面量长度 === 未定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4690520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript object literal length === undefined?
提问by Olical
I am working on this animation functionbut I have a problem. I can't seem to perform what should be an easy task, I can not get the length of an object. If you check out that jsFiddle you can see that I am running alert(properties.length);
and it is returning undefined
. Can anyone see why this might be?
我正在研究这个动画功能,但我有一个问题。我似乎无法执行应该是一项简单的任务,我无法获得对象的长度。如果您查看 jsFiddle,您可以看到我正在运行alert(properties.length);
并且正在返回undefined
。谁能看出为什么会这样?
回答by Ivo Wetzel
JavaScript object simply do nothave a length
property, only Arrays
do. If you want to know the number of properties that are defined on a object, you have to iterate over them and count them.
JavaScript对象根本就不能有一个length
属性,只有Arrays
做。如果您想知道在一个对象上定义的属性数量,您必须遍历它们并计算它们。
Also, your for in
loop is prone to bugs due extension of Object.prototype
since in will traverse the complete prototype chainand enumerate allthe properties that are on the chain.
此外,由于 in 将遍历完整的原型链并枚举链上的所有属性,因此您的for in
循环容易出现错误。Object.prototype
Example
例子
// Poisoning Object.prototype
Object.prototype.bar = 1;
var foo = {moo: 2};
for(var i in foo) {
console.log(i); // logs both 'moo' AND 'bar'
}
You have to use the hasOwnPropertymethod on the object in order to filter out those unwanted properties.
您必须在对象上使用hasOwnProperty方法才能过滤掉那些不需要的属性。
// still the foo from above
for(var i in foo) {
if (foo.hasOwnProperty(i)) {
console.log(i); // only logs 'moo'
}
}
Many JavaScript frameworks out there extend the prototype, not using hasOwnProperty
often leads to horrible bugs.
许多 JavaScript 框架都扩展了原型,不使用hasOwnProperty
通常会导致可怕的错误。
Update
更新
Concerning the actual problem that your code is not animation both properties.
关于您的代码不是动画这两个属性的实际问题。
for(var p in properties) {
...
for(var i = 0; i <= frames; i++)
{
setTimeout((function(exti, element) {
return function() {
// p gets overriden by for outer for in loop
element.style[p] = original + (pixels * exti) + 'px';
}
// you need to pass in a copy of the value of p here
// just like you do with i and element
})(i, element), i * (1000 / 60), element);
}
....
}
回答by Jamund Ferguson
This is supported in node.js and newer environments.
这在 node.js 和更新的环境中受支持。
var obj = {a: "a", b: "b"};
Object.keys(obj).length // 2
回答by Casey
If you are using Underscore.js, you can use _.size()
:
如果您使用Underscore.js,您可以使用_.size()
:
_.size({one : 1, two : 2, three : 3});
=> 3
回答by Martin Jespersen
Objects have no length, you'll need to use an array if you want that.
对象没有长度,如果需要,您需要使用数组。
If you have to find the number of properties in an object there is only one way:
如果您必须找到对象中的属性数量,只有一种方法:
var length =0;
for(var i in obj) length++;
回答by Michael McGinnis
Here's @Junaid Qadir Shekhanzai's general function for "finding the length of an object" (which as we're told, should properly be called "counting the properties of an object"). It combines solutions from @Ivo Wetzel and @Martin Jespersen:
这是@Junaid Qadir Shekhanzai 用于“查找对象的长度”的一般函数(正如我们所知,应该正确地称为“计算对象的属性”)。它结合了@Ivo Wetzel 和@Martin Jespersen 的解决方案:
function countProperties(myObj){
var length = 0;
if(typeof myObj != 'object'){
return false;
}
for(var i in myObj) {
length++;
}
return length;
}