猿问

javascript 特性

按照js按值传递
res[obj.category].bytes = '修改'
应该是不会修改__array里面的值才对,但是
res.__array.push(res[obj.category] = obj);使用这种赋值方式,修改
res[obj.category].bytes值__array里面的值会一起变更。这要理解成java的引用类型吗?

var dataObject=[ 

 { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },

 { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },

 { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },

 { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },

 { "category" : "Business", "hits" : 1, "bytes" : 2847 },

 { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },

 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },

 { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },

 { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 

];

var result = dataObject.reduce(function(res, obj) {

    if (!(obj.category in res))

        res.__array.push(res[obj.category] = obj);

    else 

        res[obj.category].bytes = '修改'

    

    return res;

}, {__array:[]})


HUWWW
浏览 393回答 1
1回答

小怪兽爱吃肉

js虽然是值传递,但是对于引用类型本质上传递的值还是指向对象的指针
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答