if (someVar.hasOwnProperty('someProperty') ) {
// do something();
} else {
// do somethingElse();
}
hasOwnProperty('someProperty')
的正确使用/解释是什么?
为什么我们不能简单地使用someVar.someProperty
来检查对象someVar
是否包含名为someProperty
的属性?
在这种情况下属性是什么?
这个javascript检查什么属性?
答案 0 :(得分:93)
hasOwnProperty
返回一个布尔值,指示您调用它的对象是否具有带参数名称的属性。例如:
var x = {
y: 10
};
console.log(x.hasOwnProperty("y")); //true
console.log(x.hasOwnProperty("z")); //false
但是,它不会查看对象的原型链。
当您使用for...in
构造枚举对象的属性时,使用它是很有用的。
如果您想查看完整的详细信息,ES5 specification一如既往地是一个值得关注的好地方。
答案 1 :(得分:15)
这是简短而准确的答案:
在javascript中,每个对象都有一堆内置的键值对,这些键值对包含有关对象的元信息。当你使用for...in
构造/循环遍历所有键值对时,你也会循环遍历这个元信息键值对(你绝对不想要)。
使用hasOwnPropery(property)
过滤这些不必要的元信息循环并直接检查参数property
是否是用户给定的属性。
通过过滤出,我的意思是,hasOwnProperty(property)
看不到,property
是否存在于对象的原型链中,即元信息。
它基于此返回布尔true/false
。
以下是一个例子:
var fruitObject = {"name": "Apple", "shape": "round", "taste": "sweet"};
console.log(fruitObject.hasOwnProperty("name")); //true
console.log(Object.prototype.hasOwnProperty("toString");) //true because in above snapshot you can see, that there is a function toString in meta-information
我希望它很清楚!
答案 2 :(得分:10)
检查:
返回一个布尔值,指示对象是否具有指定名称的属性
如果object具有指定名称的属性,则hasOwnProperty方法返回true,否则返回false。此方法不检查对象的原型链中是否存在该属性;该属性必须是对象本身的成员。
示例:
var s = new String("Sample");
document.write(s.hasOwnProperty("split")); //false
document.write(String.prototype.hasOwnProperty("split")); //true
答案 3 :(得分:5)
hasOwnProperty()
是一个可以在任何对象上调用的函数,并且将字符串作为输入。如果属性位于对象上,则返回布尔值true
,否则返回false。 hasOwnProperty()
位于Object.prototype
上,因此可用于任何对象。
function Person(name) {
this.name = name;
}
Person.prototype.age = 25;
const willem = new Person('willem');
console.log(willem.name); // property found on object
console.log(willem.age); // property found on prototype
console.log(willem.hasOwnProperty('name')); // name is on the object itself
console.log(willem.hasOwnProperty('age')); // age is not on the object itself
在此示例中,创建了一个新的Person对象。每个人都有自己的名称,该名称在构造函数中初始化。但是,年龄不是位于对象上,而是位于对象的原型上。因此,hasOwnProperty()
的名称返回true
,年龄的返回false
。
hasOwnProperty()
循环遍历对象时, for in
可能非常有用。您可以检查它的属性是否来自对象本身而不是原型。例如:
function Person(name, city) {
this.name = name;
this.city = city;
}
Person.prototype.age = 25;
const willem = new Person('Willem', 'Groningen');
for (let trait in willem) {
console.log(trait, willem[trait]); // this loop through all properties including the prototype
}
console.log('\n');
for (let trait in willem) {
if (willem.hasOwnProperty(trait)) { // this loops only through 'own' properties of the object
console.log(trait, willem[trait]);
}
}
答案 4 :(得分:2)
hasOwnProperty是一个普通的Javascript函数,它接受一个字符串参数。
在你的情况下somevar.hasOwnProperty('someProperty')它检查somevar函数是否有一些属性,它返回true和false
说
function somevar() {
this.someProperty= "Generic";
}
function welcomeMessage()
{
var somevar1= new somevar();
if(somevar1.hasOwnProperty("name"))
{
alert(somevar1.hasOwnProperty("name"));// it will return true
}
}
答案 5 :(得分:2)
您使用object.hasOwnProperty( p )来确定对象是否具有可枚举属性 p -
对象可以有自己的原型,其中'default'方法和属性被分配给每个对象实例。 hasOwnProperty仅对在构造函数中专门设置的属性返回true,或稍后添加到实例。
确定是否在任何地方为对象定义了 p ,使用if( p instanceof object),其中p计算为property-name字符串。
例如,默认情况下,所有对象都有一个'toString'方法,但它不会出现在hasOwnProperty中。
答案 6 :(得分:2)
hasOwnProperty
是检查对象是否具有属性的正确方法。 someVar.someProperty
不能替代这种情况。以下条件将显示出很好的区别:
const someVar = { isFirst: false };
// The condition is true, because 'someVar' has property 'isFirst'
if (someVar.hasOwnProperty('isFirst')) {
// Code runs
}
// The condition is false, because 'isFirst' is false.
if (someVar.isFirst) {
// Code does not runs here
}
因此someVar.isFirst
不能代替someVar.hasOwnProperty('isFirst')
。
答案 7 :(得分:0)
checks if an object has a property。据我所知,它与if(obj.prop)
的作用相同。
答案 8 :(得分:-1)
const objA = { a: 1, b: 2 }
for (const key in objA) {
if (objA.hasOwnProperty(key)) {
console.log(objA[key])
}
}
Output
1
2
const objB = {
a: 1,
b: 2,
hasOwnProperty() {
return false
}
}
for (const key in objB) {
if (objB.hasOwnProperty(key)) {
console.log(objB[key])
}
}
Outputs nothing
因为JavaScript不能保护hasOwnProperty的属性。 因此您可以像这样使用它:
for (const key in objB) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
console.log(objB[key])
}
}