JS object / array / array of objects identified

First, the subject:

  1. Object creation:

    (1) var obj = {};

    (2) var obj= new Object();  obj.name="Wang",obj.age=12

    (3) Object.create({});

  2. Object Access:

    (1) obj.a;

    (2) obj [A];   // A may be the same string, and arrays;

  3. Object methods:

    (1) obj.hasOwnPrototype ( "prop"     ); judging whether itself is a target, returns true; such as inherited attributes, to false;

    (2) obj instanceof object;     Example obj is absent Object constructor true / false;

Second, the array:

  1. Array creation:

    (1) was arr = [];

    (2) var arr=new Array(1,2,3);

    NOTE: var new new ARR = the Array (10); // 10 represents a length;

      var arr = [10.2]; //10.2 denotes one;

  2. Array method:

    1. change the original array:

      (1) .push (); was added to a final array.

      (2) .pop (); delete the last digit array, not mass transfer parameters.

      (3) .unshift (); was added to the top of the array.

      (4) .shift (); delete array front.

      (5) .reverse (); inverse array.

      (6) .splice ( from several starts, how the interception length, inserted at the cut number ) ;

      (7) .sort (); Sort

        Notes: 1. The need to write two parameter;

                  2. See Return Value: . 1) return <0; A, B

                  2) return>0; b,a

                    3) return = 0; Fixed

          Example:

            .sort(function (a,b) {

              return;

            });

    2. does not change the original array:

      (1) .concat (); splicing array

      (2) .slice ( from the intercept of the start, to the intercepting position)

      (3) .toString();

      (4) .join ( "")     ; all values within the array is connected, is connected to the symbol marks;

      (5) .split ( "")     ; striped array or variable value, as a division sign quotation marks;

  3. iterate: forEach ();

    用法:array.forEach(function(currentValue, index, arr) { }, thisValue)

    (1) currentValue: Required. The current element

    (2) index: Optional. The current index value of the element.

    (3) arr optional. The current array object element belongs.

    (4) thisValue : Optional. Value passed to the function using the general  "this" value ;

      If it is NULL,  "undefined" is passed to the  "this" value ;

      Note: You can not traverse the pseudo-array (array type);

  4. iterate: Map ();

    用法:array.forEach(function(currentValue, index, arr) { }, thisValue)

    (1) currentValue: Required. The current element

    (2) index: Optional. The current index value of the element.

    (3) arr optional. The current array object element belongs.

    (4) thisValue : Optional. Value passed to the function using the general "this" value ;

    If it is NULL, "undefined" is passed to the "this" value ;

    Note: You can not traverse the pseudo-array (array type);

 

  The array-:

    // property to be indexed (digital) properties;

    // must have a length property, the best plus push;

    var obj = {

      "0":"a",

      "1":"b",

      "2":"c",

      "length":3,

      "push":Array.prototype.push,

      "splice".Array.prototype.splice

    }

Third, the identified array [] and objects {} :

  (1) obj.constructor:

    There are a = [];

    var b = {};

    a.constructor;    //Array()

    b.constructor;    //Object()

  (2) obj instanceof object:

    There are a = [];

    var b = {};

    a instanceof Array;    //true

    b instanceof Array;    //false

  (3) Object.prototype.toString.call( [] );    //"[object Array]"

        Object.prototype.toString.call( {} );    //"[object Object]"

Guess you like

Origin www.cnblogs.com/Tractors/p/11079265.html