JavaScript in operator

Source: Internet
Author: User

The in operator of JavaScript can be used to determine whether an attribute belongs to an object, or it can be used to variable an object's properties

1. Judging attributes belong to object

var mycar = {make: "Honda", Model: "Accord", year:1998}; // Note that the property name must be in the form of a string because make is not a variable if inch Mycar) {    document.write (' true ');} Else {    document.write (' false ');  // Display True

2. For in Traversal object properties

var mycar = {make: "Honda", Model: "Accord", year:1998};  for (var in mycar) {    + ': ' + make[p]);}

3. In can be used to judge

if (foo = = ' Bar ' | | foo = = ' Foobar ' | | foo = = ' foo ' ) {//... }// can be written as:if in {' Bar ': ', ' foobar ': ', ' foo ': '} ' ) {// ...}

4. The delete operator can delete properties, mate with in

var mycar = {make: "Honda", Model: "Accord", year:1998}; Delete Mycar.make;  in Mycar;  // returns false var New Array ("Redwood", "Bay", "Cedar", "oak", "maple"); Delete trees[3]; inch // returns false

5. Try not to iterate through the array with for

The efficiency of collection traversal is: hash > for (;;) > for (in)

Sets the preferred object for the associated operation, followed by a for loop traversal.

Reference: http://www.linuxfly.org/post/524/

JavaScript in operator

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.