最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Safest way to loop over JavaScript object - Stack Overflow

programmeradmin2浏览0评论

I want to create an object to hold a bunch of properties like

var o = {x: 20, y: 40, color: 'red'};

More properties can be added to the object throughout its life, so I would like a generic way to loop over the properties to set them later. I know if the Object.prototype has been messed with, simply

for(var prop in o) { ... }

will cause its properties to also be looped over. What other things can cause this to mess up, and what is the safest way to loop like this?

I want to create an object to hold a bunch of properties like

var o = {x: 20, y: 40, color: 'red'};

More properties can be added to the object throughout its life, so I would like a generic way to loop over the properties to set them later. I know if the Object.prototype has been messed with, simply

for(var prop in o) { ... }

will cause its properties to also be looped over. What other things can cause this to mess up, and what is the safest way to loop like this?

Share Improve this question edited Mar 29, 2017 at 2:43 Jeremy Banks - Vive le Canada 130k88 gold badges358 silver badges381 bronze badges asked Jul 28, 2011 at 22:12 Davis DimitriovDavis Dimitriov 4,2573 gold badges33 silver badges46 bronze badges 2
  • Take a look at [this post][1], you pretty much said it in your question. [1]: stackoverflow./questions/242841/javascript-foreach-vs-for – George P Commented Jul 28, 2011 at 22:15
  • @Davis if you follow the link from George P pay attention to the answer by Bno because it, and not the accepted answer, mentions the hasOwnProperty technique which saves you from the case in which Object.prototype has been messed with, and is considered a best practice, at least by Good Parts adherents. – Ray Toal Commented Jul 28, 2011 at 22:21
Add a ment  | 

3 Answers 3

Reset to default 7

If you want to iterate over all of the properties on an object, then inherited properties are the only real problem you have to deal with. The Object.prototype.hasOwnProperty() method can be used to confirm that a property exists directly an object, and isn't just inherited.

Object.prototype.example = 2;
var o = {x: 4, y: 6};

for (var prop in o) {
    if (Object.prototype.hasOwnProperty.call(o, prop)) {
        console.log(o[prop]);
    }
}

This will print all of the properties of the object itself, but it won't print the inherited .example.

As an alternative to what @Jeremy Banks suggested, Object.keys method does return only "own" property names of an object, too. Hence an ES5 solution:

var o = { x: 20, y: 40, color: 'red' };

Object.keys(o).forEach(function (prop) { /* ... */ });

Use object.hasOwnProperty()

var buz = {
    fog: 'stack'
};

for (var name in buz) {
    if (buz.hasOwnProperty(name)) {
        alert("this is fog (" + name + ") for sure. Value: " + buz[name]);
    }
    else {
        alert(name); // toString or something else
    }
}

https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty#Example.3a_Iterating_over_the_properties_of_an_object

发布评论

评论列表(0)

  1. 暂无评论