The in operator

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.

// 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 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 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 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 false

var 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

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.
StandardInitial definition. Implemented in JavaScript 1.4.

Browser compatibility

Feature Chrome Firefox (Gecko) Edge Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

Here is the function template for `isEqualTo`: ``` template<typename T> bool isEqualTo(const T& a, const T& b) { return a == b; } ``` We can use this function template in a program to compare built-in types like so: ``` #include <iostream> int main() { int a = 5, b = 7; std::cout << isEqualTo(a, b) << std::endl; // false double c = 3.14, d = 3.14; std::cout << isEqualTo(c, d) << std::endl; // true char e = 'a', f = 'b'; std::cout << isEqualTo(e, f) << std::endl; // false return 0; } ``` When we attempt to run this program with a user-defined class type `Complex` without overloading the equality operator, we will get a compilation error because the compiler does not know how to compare two `Complex` objects with the equality operator. Once we overload the equality operator for the `Complex` class, the program will compile and execute without errors, allowing us to compare `Complex` objects using the `isEqualTo` function template. For example: ``` #include <iostream> class Complex { public: Complex(double r, double i) : real(r), imag(i) {} double real, imag; }; bool operator==(const Complex& a, const Complex& b) { return a.real == b.real && a.imag == b.imag; } int main() { Complex x(1.0, 2.0), y(1.0, 2.0); std::cout << isEqualTo(x, y) << std::endl; // true Complex z(3.0, 4.0); std::cout << isEqualTo(x, z) << std::endl; // false return 0; } ``` In this program, we have defined a `Complex` class with a constructor that initializes its real and imaginary parts. We have also overloaded the equality operator to compare two `Complex` objects by their real and imaginary parts. We can now use the `isEqualTo` function template to compare `Complex` objects and get the expected results.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值