JavaScript in operator __java

Source: Internet
Author: User
Definition:

The in operator is used to determine that an attribute belongs to an object, either as a direct property of an object or as an inherited property through prototype. (See hasOwnProperty) Precautions :

n for a Generic object property, you need to specify the name of the property with a string

Such as:
var Mycar = {make: "Honda", Model: "Accord", year:1998};
' Make ' in Mycar//returns True
"Model" in Mycar//returns True

n an indexed value that specifies a numeric form for an array property to represent the property name of the array (except for intrinsic attributes, such as length).

Arrays
var trees = new Array ("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 in that index)
' Length ' in Trees//returns True (the length is a Array property)

The right side of N in must be an object, such as: You can specify a form that is generated with the string constructor, but you cannot specify a direct amount of string:

var color1 = new String ("green");
' Length ' in Color1//returns True
var color2 = "Coral";
"Length" in Color2//generates an error (color isn't a String object)

n If you Delete an attribute using the delete operator, it returns falseif you use the in check again,such as:

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

n If you set a property value to undefinedbut do not use the delete operator, using in check will return true.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.