Javascript hasOwnProperty中的属性是什么?

编程入门 行业动态 更新时间:2024-10-18 16:52:58
本文介绍了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中的属性是什么?

本文发布于:2023-11-02 14:07:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   Javascript   hasOwnProperty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!