JavaScript 中 hasOwnProperty 中的属性是什么?

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

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

发布评论

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

>www.elefans.com

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