Javascript hasOwnProperty中的属性是什么? [英] Javascript what is property in hasOwnProperty?

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

问题描述

if (someVar.hasOwnProperty('someProperty') ) {
 // do something();
} else {
 // do somethingElse();
}

正确使用/解释hasOwnProperty( 'someProperty')

为什么我们不能简单地使用 someVar.someProperty 检查对象 someVar 是否包含名称为 someProperty 的属性?

Why we can't 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.

使用枚举构造中的...的对象属性时,使用它是很有用的。

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天全站免登陆