JavaScript 中 hasOwnProperty 中的属性是什么? [英] What is property in hasOwnProperty in JavaScript?

查看:29
本文介绍了JavaScript 中 hasOwnProperty 中的属性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

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 中的属性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆