The in operator
来源:互联网 发布:跑跑卡丁车mac可以玩吗 编辑:程序博客网 时间:2024/05/22 05:22
The in
operator returns true
if the specified property is in the specified object.
Syntax
prop in objectName
Parameters
prop
- A string or symbol representing a property name or array index (non-symbols will be coerced to strings).
objectName
- Name of an object.
Description
The following examples show some uses of the in
operator.
// Arraysvar trees = ["redwood", "bay", "cedar", "oak", "maple"];0 in trees // returns true3 in trees // returns true6 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 objectsvar 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 in
operator. For example, you can specify a string created with the String
constructor, but you cannot specify a string literal.
var color1 = new String("green");"length" in color1 // returns truevar 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 delete
operator, the in
operator returns false
for that property.
var mycar = {make: "Honda", model: "Accord", year: 1998};delete mycar.make;"make" in mycar; // returns falsevar trees = new Array("redwood", "bay", "cedar", "oak", "maple");delete trees[3];3 in trees; // returns false
If you set a property to undefined
but do not delete it, the in
operator 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 in
operator returns true
for properties in the prototype chain.
"toString" in {}; // returns true
Specifications
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.StandardInitial definition. Implemented in JavaScript 1.4.
Browser compatibility
- Desktop
- Mobile
See also
delete
Object.prototype.hasOwnProperty()
Reflect.has()
- Enumerability and ownership of properties
0 0
- The in operator
- The "tr///" operator in Perl
- the operator of file in web
- The Safe Navigation Operator (&.) in Ruby
- comparing strings in PHP with the == operator
- detects edges in an image using the homogeneity operator
- What is the !! (not not) operator in JavaScript?
- Question 28: Which of the following statements accurately describe the new[] operator in C++?
- Except operator in MSSQL2005
- Comparison operator in shell
- Let operator in linqtoentities
- diamond operator in Perl
- conditional operator in Xcode
- ... operator in ES6
- overload the operator
- Explaining the PeopleCode @ operator
- qmake "The *= operator"
- perl the special operator
- Python点滴(六)
- python win系统获取磁盘及文件夹大小
- 用vnc实现windows远程连接linux桌面
- #define相关
- https php使用curl获取https请求的方法
- The in operator
- DataFrame和SparkSql使用区别
- VMware12 + Ubuntu16.04 虚拟磁盘扩容
- Unity读取 JSon配置文件
- 浏览器缩小页面,布局就乱了的解决方法
- Android中OnScrollListener的详解(Listview分批加载用到
- JavaScript学习笔记七:iterable
- maven环境准备
- RxJava操作符(06-错误处理)