hasOwnProperty()
2016-06-13 15:21
399 查看
The
returns a boolean indicating whether the object has the specified property.
Every object descended from
the
has the specified property as a direct property of that object; unlike the
this method does not check down the object's prototype chain.
The following example determines whether the
The following example differentiates between direct properties and properties inherited through the prototype chain:
The following example shows how to iterate over the properties of an object without executing on inherit properties. Note that the
is already only iterating enumerable items, so one should not assume based on the lack of non-enumerable properties shown in the loop that
is confined strictly to enumerable items (as with
JavaScript does not protect the property name
exists that an object might have a property with this name, it is necessary to use an external
get correct results:
Note that in the last case there are no newly created objects.
Desktop
Mobile
Enumerability
and ownership of properties
JavaScript
Guide: Inheritance revisited
hasOwnProperty()method
returns a boolean indicating whether the object has the specified property.
Syntax
obj.hasOwnProperty(prop)
Parameters
propThe name of the property to test.
Description
Every object descended from Objectinherits
the
hasOwnPropertymethod. This method can be used to determine whether an object
has the specified property as a direct property of that object; unlike the
inoperator,
this method does not check down the object's prototype chain.
Examples
Using hasOwnProperty
to test for a property's existence
The following example determines whether the oobject contains a property named
prop:
o = new Object(); o.prop = 'exists'; function changeO() { o.newprop = o.prop; delete o.prop; } o.hasOwnProperty('prop'); // returns true changeO(); o.hasOwnProperty('prop'); // returns false
Direct versus inherited properties
The following example differentiates between direct properties and properties inherited through the prototype chain:o = new Object(); o.prop = 'exists'; o.hasOwnProperty('prop'); // returns true o.hasOwnProperty('toString'); // returns false o.hasOwnProperty('hasOwnProperty'); // returns false
Iterating over the properties of an object
The following example shows how to iterate over the properties of an object without executing on inherit properties. Note that the for...inloop
is already only iterating enumerable items, so one should not assume based on the lack of non-enumerable properties shown in the loop that
hasOwnPropertyitself
is confined strictly to enumerable items (as with
Object.getOwnPropertyNames()).
var buz = { fog: 'stack' }; for (var name in buz) { if (buz.hasOwnProperty(name)) { console.log('this is fog (' + name + ') for sure. Value: ' + buz[name]); } else { console.log(name); // toString or something else } }
Using hasOwnProperty
as a property name
JavaScript does not protect the property name hasOwnProperty; thus, if the possibility
exists that an object might have a property with this name, it is necessary to use an external
hasOwnPropertyto
get correct results:
var foo = { hasOwnProperty: function() { return false; }, bar: 'Here be dragons' }; foo.hasOwnProperty('bar'); // always returns false // Use another Object's hasOwnProperty and call it with 'this' set to foo ({}).hasOwnProperty.call(foo, 'bar'); // true // It's also possible to use the hasOwnProperty property from the Object prototype for this purpose Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
Note that in the last case there are no newly created objects.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.5. |
ECMAScript 5.1 (ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. | Standard | |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Object.prototype.hasOwnProperty' in that specification. | Draft |
Browser compatibility
Desktop Mobile
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
See also
Enumerabilityand ownership of properties
Object.getOwnPropertyNames()
for...in
in
JavaScript
Guide: Inheritance revisited
相关文章推荐
- laravel5.0在linux环境下解决.htaccess无效和去除index.php
- nginx.conf配置详解
- Linux学习笔记(2)
- Linux下TI omap芯片 MUX 配置分析(以AM335X芯片为例)
- Linux-SVN 常用命令详解
- centos gitlab安装
- Linux学习笔记(1)
- Linux下GitLab的快速安装以及备份
- go1.6.2 linux/amd64 的一个bug: gcc: 无法识别的选项‘-no-pie’
- Apache Hadoop YARN架构简介
- Hadoop2.2.0已经遗弃的属性名称
- linux 的重定向
- linux 查看静态库,动态库是32位还是64位
- adb shell 调试 Android 串口
- Linux fd_set结构用法
- Docker 镜像、容器
- <OJ_Sicily>Pythagorean Proposition
- Linux-systemctl命令
- kafka彻底删除topic
- CentOS环境下使用javaCV