"JS Authoritative Guide Learning Summary--6.5 enumeration Properties"

Source: Internet
Author: User

Content Highlights:

I. For/in cycle

The 1.for/in loop iterates through all the enumerable properties of the object (including its own and inherited properties) in the loop body, assigning the property name to the loop variable. The built-in methods for object inheritance are not enumerable, but the properties that are added to the object in code are enumerable (unless you convert them to non-enumerable using one of the methods mentioned below). For example:

var o = {x:1, y:2,z:3}//Three enumerable properties

O.propertyisenumberable ("toString"); =>false, non-enumerable

For (P in O)//Traversal property

Console.log (P); Outputs x, Y and Z, and does not output ToString

2. Before ES 5, some newly added methods cannot be defined as non-enumerable, so they can all be enumerated in the For/in loop. To avoid this situation, you need to filter the properties returned by the For/in loop, which are the most common:

For (P in O) {

if (!o.hasownproperty (p)) continue; Skip inherited properties

}

For (P in O) {

if (typeof o[p]=== "function") continue; Skip method

}

Two. Some useful tool functions manipulate the properties of objects, which use the for/in loop.

Object tool functions to enumerate the properties:

/* Copy the enumerable attribute in P to O and return to O.  If O and P contain attributes with the same name, the properties in O are overwritten. This function does not handle getter and setter and copy properties */

function Extend (o,p) {

For (prop in P) {//Traverse all properties in P

O[prop]=p[prop]; To add a property to P

}

return o;

}

/* Copy the enumerable attribute in P to O and return to O. If O and P have properties with the same name, the properties in O will not be affected. This function does not come out getter and setter and copy properties */

Funtion Merge (o,p) {

For (prop in P) {//Traverse all properties in P

if (O.hasownproperty[prop]) continue; Filter out properties that already exist in O

O[prop]=p[prop]; To add an attribute to the O

}

return o;

}

/* If the property in O does not have the same name attribute in P, remove this property from O and return o*/

function Restrict (o,p) {

For (prop in P) {//Traverse all properties in O

if (! (   Prop in P)) Delete O[prop]; If it does not exist in P, delete the

}

return o;

}

/* If the property in O has the same name attribute in P, remove this property from O and return o*/

function Substract (o,p) {

For (prop in P) {//Traverse all properties in P

Delete O[prop]//Remove from O (Deleting a nonexistent property will not error)

}

return o;

}

/* Returns a new object that has both the properties of O and the properties of P, and if there is a duplicate attribute in O and p, use the attribute value in P */

Function Union (O,P) {return extend (Extend ({},o), p);}

/* Returns a new object that has properties that appear in both O and P, much like the intersection of O and P, but the value of the property in P is ignored */

function intersection (O,P) {return Restrict (extend ({},o), p);}

/* Returns an array containing the names of enumerable properties in O */

function keys (o) {

if (typeof o!== "Object") throw TypeError (); Parameter must be an object

var result = []; The array that will be returned

For (Var prop in O) {//Iterate through all enumerable properties

if (O.hasownproperty (prop))//Determine if the property is owned

Result.push (prop); Add a property name to the array

}

return result; returns this array

}

Three.

In addition to the For/in loop, ES 5 defines two functions to enumerate the name of a property. The first is Object.keys (), which returns an array that consists of an enumerable of its own property names in the object.

The function of the second enumeration property in ES 5 is object.getownpropertynames (), which is similar to Object.keys (), except that it returns all the names of its own properties, not just the enumerable properties.

"JS Authoritative Guide Learning Summary--6.5 enumeration Properties"

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.