测试JavaScript对象上是否存在属性的正确方法是什么?

时间:2022-10-22 11:19:44

I have a custom Javascript object that I create with new, and assign properties to based on creation arguments:

我有一个我用new创建的自定义Javascript对象,并根据创建参数分配属性:

function MyObject(argument) {
    if (argument) {
        this.prop = "foo";
    }
}
var objWithProp = new MyObject(true); // objWithProp.prop exists
var objWithoutProp = new MyObject(false); // objWithoutProp.prop does not exist

What's the correct way to test for the existence of the prop property of the objects? I've seen the following ways used, but I'm not sure if any of these ways is the best way:

测试对象prop属性是否存在的正确方法是什么?我已经看到了以下使用的方法,但我不确定这些方法中的任何一种是否是最佳方式:

  • if (obj.prop) {}
  • if(obj.prop){}

  • if (obj.hasOwnProperty("prop")) {}
  • if(obj.hasOwnProperty(“prop”)){}

  • if ("prop" in obj) {}
  • if(obj中的“prop”){}

Specifically, I'm only interested in testing if the property is explicitly defined for this object, not in the prototype chain. In addition, the value will never be set to null or undefined, but it could be something like an empty object or array. However, if you want to include what the correct way is if those could be the case, feel free.

具体来说,我只对测试是否为此对象明确定义属性感兴趣,而不是在原型链中。此外,该值永远不会设置为null或undefined,但它可能类似于空对象或数组。但是,如果你想包括正确的方法,如果可能的话,请随意。

3 个解决方案

#1


hasOwnProperty is exactly what you're looking for, since you specify "if the property is explicitly defined for this object, not in the prototype chain". Per https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty , "This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain." -- seems to exactly match your requirement!

hasOwnProperty正是您正在寻找的,因为您指定“如果为此对象显式定义属性,而不是在原型链中”。根据https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty,“此方法可用于确定对象是否具有指定属性作为该对象的直接属性;与in运算符不同,这种方法不会检查对象的原型链。“ - 似乎完全符合您的要求!

#2


If you are looking for a property defined in an object, you can use hasOwnProperty method of the object. like this:

如果要查找对象中定义的属性,可以使用该对象的hasOwnProperty方法。像这样:

myObject = new MyObject();
// some code
if ( myObject.hasOwnProperty('prop') ) {
    // prop exists
}

but this is only to know if such a property is defined in object itself, but not its parents. so if such property is inherited by the object, you can not test its existence like this.

但这只是要知道对象本身是否定义了这样的属性,而不是它的父节点。所以如果这个属性是由对象继承的,你就不能像这样测试它的存在。

the other way is to test the property against undefined value. like this:

另一种方法是针对未定义的值测试属性。像这样:

if ( myObject.prop !== undefined ) {
    // prop exists
}

remember to use the !== operator instead of != because != will not differ between null and undefined, but !== does. so if your object has a property but the value is null, != will not help you. so this test:

记得使用!==运算符而不是!=因为!=在null和undefined之间没有区别,但是!==确实如此。因此,如果您的对象具有属性但值为null,则!=将无法帮助您。所以这个测试:

if ( myObject.prop ) {
}

might have wrong results if "prop" has a false or null value. but by comparing to undefined with !== operator, you can be sure that null/false values will not confuse you.

如果“prop”具有false或null值,则可能有错误的结果。但通过使用!==运算符与undefined进行比较,您可以确定null / false值不会让您感到困惑。

#3


You can check the existence of a variable as follows:

您可以检查变量的存在,如下所示:

if ( typeof variable === 'undefined' || variable === null ) {
    // Do stuff
}

It's also can be used for properties.

它也可以用于属性。

#1


hasOwnProperty is exactly what you're looking for, since you specify "if the property is explicitly defined for this object, not in the prototype chain". Per https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty , "This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain." -- seems to exactly match your requirement!

hasOwnProperty正是您正在寻找的,因为您指定“如果为此对象显式定义属性,而不是在原型链中”。根据https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty,“此方法可用于确定对象是否具有指定属性作为该对象的直接属性;与in运算符不同,这种方法不会检查对象的原型链。“ - 似乎完全符合您的要求!

#2


If you are looking for a property defined in an object, you can use hasOwnProperty method of the object. like this:

如果要查找对象中定义的属性,可以使用该对象的hasOwnProperty方法。像这样:

myObject = new MyObject();
// some code
if ( myObject.hasOwnProperty('prop') ) {
    // prop exists
}

but this is only to know if such a property is defined in object itself, but not its parents. so if such property is inherited by the object, you can not test its existence like this.

但这只是要知道对象本身是否定义了这样的属性,而不是它的父节点。所以如果这个属性是由对象继承的,你就不能像这样测试它的存在。

the other way is to test the property against undefined value. like this:

另一种方法是针对未定义的值测试属性。像这样:

if ( myObject.prop !== undefined ) {
    // prop exists
}

remember to use the !== operator instead of != because != will not differ between null and undefined, but !== does. so if your object has a property but the value is null, != will not help you. so this test:

记得使用!==运算符而不是!=因为!=在null和undefined之间没有区别,但是!==确实如此。因此,如果您的对象具有属性但值为null,则!=将无法帮助您。所以这个测试:

if ( myObject.prop ) {
}

might have wrong results if "prop" has a false or null value. but by comparing to undefined with !== operator, you can be sure that null/false values will not confuse you.

如果“prop”具有false或null值,则可能有错误的结果。但通过使用!==运算符与undefined进行比较,您可以确定null / false值不会让您感到困惑。

#3


You can check the existence of a variable as follows:

您可以检查变量的存在,如下所示:

if ( typeof variable === 'undefined' || variable === null ) {
    // Do stuff
}

It's also can be used for properties.

它也可以用于属性。