JavaScript 对象字面量长度 === 未定义?

发布于 2024-10-12 04:23:35 字数 231 浏览 5 评论 0原文

我正在研究这个动画功能,但我有一个问题。我似乎无法执行应该是一项简单的任务,我无法获得对象的长度。如果您检查 jsFiddle,您可以看到我正在运行 alert(properties.length); 并且它返回 undefined。谁能明白为什么会这样吗?

I am working on this animation function but 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

柏拉图鍀咏恒 2024-10-19 04:23:36

这在 Node.js 和较新的环境中受支持。

var obj = {a: "a", b: "b"};
Object.keys(obj).length // 2

This is supported in node.js and newer environments.

var obj = {a: "a", b: "b"};
Object.keys(obj).length // 2
樱娆 2024-10-19 04:23:36

JavaScript 对象没有length 属性,只有Arrays 有。如果您想知道对象上定义的属性数量,则必须迭代它们并对它们进行计数。

此外,由于 Object.prototype 的扩展,您的 for in 循环很容易出现错误,因为 in 将遍历完整的原型链并枚举链上的所有属性。

示例

// Poisoning Object.prototype
Object.prototype.bar = 1;

var foo = {moo: 2};
for(var i in foo) {
    console.log(i); // logs both 'moo' AND 'bar'
}

您必须使用hasOwnProperty 对象上的方法以过滤掉那些不需要的属性。

// still the foo from above
for(var i in foo) {
    if (foo.hasOwnProperty(i)) {
        console.log(i); // only logs 'moo'
    }
}

许多 JavaScript 框架都扩展了原型,不使用 hasOwnProperty 通常会导致可怕的错误。

更新

关于您的代码不是动画这两个属性的实际问题。

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);
    }
    ....
 }

JavaScript object simply do not have 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.

Also, your for in loop is prone to bugs due extension of Object.prototype since in will traverse the complete prototype chain and enumerate all the properties that are on the chain.

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 hasOwnProperty method on the object in order to filter out those unwanted properties.

// 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.

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);
    }
    ....
 }
在巴黎塔顶看东京樱花 2024-10-19 04:23:36

如果您使用 Underscore.js,则可以使用 _.size() :

_.size({one : 1, two : 2, three : 3});
=> 3

If you are using Underscore.js, you can use _.size():

_.size({one : 1, two : 2, three : 3});
=> 3
流殇 2024-10-19 04:23:36

对象没有长度,如果需要,您需要使用数组。

如果您必须查找对象中属性的数量,只有一种方法:

var length =0;
for(var i in obj) length++;

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++;
人心善变 2024-10-19 04:23:36

这是@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;
}

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:

function countProperties(myObj){
    var length = 0;
    if(typeof myObj != 'object'){
        return false;
    }
    for(var i in myObj) {
    length++;
    }
    return length;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文