javascript - 如何给js的对象动态增加key?
阿神
阿神 2017-04-10 14:50:08
[JavaScript讨论组]

我有这样一段代码

_.each(course, function(value, key) {
  course[key+"test"] = value + 1;
});

这段代码在chrome下,是ok的。但是ie8下会报内存溢出。如果去掉test,就好了。

_.each(course, function(value, key) {
  course[key] = value + 1;
});

求大神指点一下。谢谢了。

阿神
阿神

闭关修行中......

全部回复(2)
大家讲道理

不知道LZ的_.each是什么,很有可能是因为foreach循环会动态查询course的所有键值,由于键值不断增多,造成了死循环,造成内存溢出。

比如course是{a:1},死循环过程就是这样:

course["atest"] = 2;
course["atesttest"] = 3;
course["atesttesttest"] = 4;
course["atesttesttesttest"] = 5;
// ...

ECMAScript的标准里面未定义for-in遍历顺序,新增键是否被遍历完全依赖于UA,无法强制保证新增的键会/不会被遍历到。

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified. Properties of the object being enumerated may be deleted during enumeration. If a property that has not yet been visited during enumeration is deleted, then it will not be visited. If new properties are added to the object being enumerated during enumeration, the newly added properties are not guaranteed to be visited in the active enumeration. A property name must not be visited more than once in any enumeration.

underscore避免这个死循环的方法是首先用一个数组缓存当前的keys(ECMAScript 5里面有Object.keys,fallback方法则是for in循环),然后循环keys数组,这样在遍历过程中增加的key不会被遍历到。

function getKeys(obj) {
    if(Object.keys) {
        return Object.keys(obj) ;
    }
    var keys = [];
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            keys.push(key);
        }
    }
    return keys;
}
var course = {a: 1},
    courseKeys = getKeys(course);
for( var i = 0 ; i < courseKeys.length; i++ ) {
    course[courseKeys[i]+"test"] = i ;
}
高洛峰

换一种写法

javascriptvar tmpCourse = {};
_.each(course, function(value, key) {
  tmpCourse[key] = value;
  tmpCourse[key+"test"] = value + 1;
});
course = tmpCourse;
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号