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

Javascript setattr or setOwnProperty - Stack Overflow

programmeradmin1浏览0评论

I have a simple array:

var arr = ['has_cats', 'has_dogs'];

and an object:

var obj = new Object();

and from the array I want to set object attributes:

for( i=0; i < arr.length; i++ ) {
        if(!arr.hasOwnProperty(arr[i])) {
                // set the object property
        }
}

After looping I should be able to call obj.has_cats but I can't seem to find a proper way to do it in javascript. In python I would call setattr(obj,arr[i], value). I figured that if objects have a hasOwnProperty they should also have a getOwnProperty and a setOwnProperty.

Any guidance?

I have a simple array:

var arr = ['has_cats', 'has_dogs'];

and an object:

var obj = new Object();

and from the array I want to set object attributes:

for( i=0; i < arr.length; i++ ) {
        if(!arr.hasOwnProperty(arr[i])) {
                // set the object property
        }
}

After looping I should be able to call obj.has_cats but I can't seem to find a proper way to do it in javascript. In python I would call setattr(obj,arr[i], value). I figured that if objects have a hasOwnProperty they should also have a getOwnProperty and a setOwnProperty.

Any guidance?

Share Improve this question asked Aug 3, 2011 at 3:14 Romeo M.Romeo M. 3,2788 gold badges36 silver badges42 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

"I figured that if objects have a hasOwnProperty they should also have a getOwnProperty and a setOwnProperty"

The hasOwnProperty() function tells you whether the named property exists as a direct property of the object, as pared to being an inherited property from somewhere in the object's prototype chain. The in operator - used like if (someProperty in someObject) {} - will tell you whether the object has that property anywhere in the prototype chain.

You don't need a corresponding setOwnProperty() function because you can just say:

someObject[someProperty] = someValue;

I guess the idea of a corresponding getOwnProperty() function kind of makes sense if you want a function that only returns a value if the specified property is a direct property, but then there wouldn't be any way to indicate that the specified property wasn't found because null, undefined, false, etc. are all legitimate potential values if the property is found. So to achieve that you need to do it as a two-step process using if (hasOwnProperty()).

But it doesn't sound like that's what you're trying to do. If I understand you correctly, you just want some way to set a property where the property name is in a variable (in your case, an array element). You don't make it clear what values you want associated to those properties, so I'll just use true.

var arr = ['has_cats', 'has_dogs'];

var obj = {}; // note {} is generally preferred to new Object();

for(var i=0; i < arr.length; i++ ) {
  // if the property doesn't already exist directly on
  // the object
  if(!obj.hasOwnProperty(arr[i])) {
     // set the object property
     obj[arr[i]] = true;
  }
}

// can access with bracket notation
alert(obj["has_cats"]);

// can access with dot notation
alert(obj.has_cats);

You can set the value via:

for(i = 0; i < arr.length; i++ ) {
        if(!obj.hasOwnProperty(arr[i])) {
                obj[arr[i]] = 'value';
        }
}

Neither getOwnProperty nor setOwnProperty would add any value:

The following always set the property on the object that evaluated from x. Therefore, setOwnProperty is nothing more but a property assignment.

x[prop] = v

Likewise, using a bination of hasOwnProperty and property existence it is possible to derive (and supersede) getOwnProperty:

if (x.hasOwnProperty(prop)) {
   // x has prop
   // x[prop] may evaluate to undefined
} else if (prop in x) {
   // prop resolved through [[prototype]] chain
   // x[prop] may evaluate to undefined
} else {
   // property prop not set
   // x[prop] will evaluate to undefined
}

Happy coding.

I think you are overplicating this somewhat. Try this:

var arr = ['has_cats', 'has_dogs'];
var obj = new Object();
for( i=0; i < arr.length; i++ ) {
    obj[arr[i]] = true;
}

You don't need to use hasOwnProperty against an array when iterating it by its indices. Though perhaps you meant to check against obj as a guard against overwriting anything that was already set?

You can set the property with:

obj[arr[i]] = "my value";

In javascript property access can be either done by .name or ['name'].

发布评论

评论列表(0)

  1. 暂无评论