考虑:
if (someVar.hasOwnProperty('someProperty') ) { // Do something(); } else { // Do somethingElse(); }hasOwnProperty('someProperty')的正确用法/解释是什么?
What is the right use/explanation of hasOwnProperty('someProperty')?
为什么不能简单地使用someVar.someProperty检查对象someVar是否包含名称为someProperty的属性?
Why can't we simply use someVar.someProperty to check if an object someVar contains property with name someProperty?
在这种情况下,什么是财产?
What is a property in this case?
此JavaScript检查哪些属性?
What property does this JavaScript check?
推荐答案hasOwnProperty返回一个布尔值,该布尔值指示您所调用的对象是否具有带有参数名称的属性.例如:
hasOwnProperty returns a boolean value indicating whether the object on which you are calling it has a property with the name of the argument. For example:
var x = { y: 10 }; console.log(x.hasOwnProperty("y")); //true console.log(x.hasOwnProperty("z")); //false但是,它不会查看对象的原型链.
However, it does not look at the prototype chain of the object.
在通过for...in构造枚举对象的属性时使用它很有用.
It's useful to use it when you enumerate the properties of an object with the for...in construct.
如果您想查看全部详细信息,那么 ES5规范和往常一样是个好地方看.
If you want to see the full details, the ES5 specification is, as always, a good place to look.
更多推荐
JavaScript中hasOwnProperty中的属性是什么?
发布评论