The in operator
2016-06-13 15:40
337 查看
The
the specified property is in the specified object.
The following examples show some uses of the
You must specify an object on the right side of the
can specify a string created with the
a string literal.
If you delete a property with the
the
that property.
If you set a property to
do not delete it, the
The
properties in the prototype chain.
Desktop
Mobile
Enumerability
and ownership of properties
inoperator returns
trueif
the specified property is in the specified object.
Syntax
prop in objectName
Parameters
propA string or symbol representing a property name or array index (non-symbols will be coerced to strings).
objectNameName of an object.
Description
The following examples show some uses of the inoperator.
// Arrays var trees = ["redwood", "bay", "cedar", "oak", "maple"]; 0 in trees // returns true 3 in trees // returns true 6 in trees // returns false "bay" in trees // returns false (you must specify the // index number, not the value at that index) "length" in trees // returns true (length is an Array property) Symbol.iterator in trees // returns true (arrays are iterable, works only in ES6+) // Predefined objects "PI" in Math // returns true // Custom objects var mycar = {make: "Honda", model: "Accord", year: 1998}; "make" in mycar // returns true "model" in mycar // returns true
You must specify an object on the right side of the
inoperator. For example, you
can specify a string created with the
Stringconstructor, but you cannot specify
a string literal.
var color1 = new String("green"); "length" in color1 // returns true var color2 = "coral"; // generates an error (color2 is not a String object) "length" in color2
Using in
with deleted or undefined properties
If you delete a property with the deleteoperator,
the
inoperator returns
falsefor
that property.
var mycar = {make: "Honda", model: "Accord", year: 1998}; delete mycar.make; "make" in mycar; // returns false var trees = new Array("redwood", "bay", "cedar", "oak", "maple"); delete trees[3]; 3 in trees; // returns false
If you set a property to
undefinedbut
do not delete it, the
inoperator returns true for that property.
var mycar = {make: "Honda", model: "Accord", year: 1998}; mycar.make = undefined; "make" in mycar; // returns true
var trees = new Array("redwood", "bay", "cedar", "oak", "maple"); trees[3] = undefined; 3 in trees; // returns true
Inherited properties
The inoperator returns
truefor
properties in the prototype chain.
"toString" in {}; // returns true
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2017 Draft (ECMA-262) The definition of 'Relational Operators' in that specification. | Draft | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Relational Operators' in that specification. | Standard | |
ECMAScript 5.1 (ECMA-262) The definition of 'The in Operator' in that specification. | Standard | |
ECMAScript 3rd Edition (ECMA-262) The definition of 'The in Operator' in that specification. | Standard | Initial definition. Implemented in JavaScript 1.4. |
Browser compatibility
Desktop Mobile
Feature | Chrome | Firefox (Gecko) | Edge | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
See also
delete
Object.prototype.hasOwnProperty()
Reflect.has()
Enumerability
and ownership of properties
相关文章推荐
- 用vnc实现windows远程连接linux桌面
- 接触到的前端网站(仅个人收藏用)
- Linux命令总结
- xshell如何正确来连接远程Linux服务器?
- 2016.6.13 通过虚拟机安装hadoop完全分布式集群
- ctags+taglist+cscope
- Xshell5中文破解版
- linux内存寻址——内存管理
- Linux select()函数
- Linux学习笔记(3)
- 使用秘钥对登录Linux系统
- linux工具---用qemu调试linux内核
- Linux基础
- Linux源代码目录
- linux内存管理
- Linux初学 - head,tail,grep,sed,yum,find
- 让XShell保存日志教程
- 利用OpenCV的imread将RGB图像转化为灰度图像!
- convert命令常用图片操作
- hasOwnProperty()