Reading Notes: in-depth understanding ES6 (seven)

Chapter 7 Set collections and collections Map

Preface: In ES5, developers have been using non-array object implements a similar set of Set, a similar function Map collection. In ES6, a new set of standards will Set Map collection added to JavaScript.


Set set of Map Section 1 ES5 in the collection
    
    in ES5, developers often use object properties to simulate the combination of the two. In general, Set collection is often used if there is a certain key name in the examination subject, and is often used to obtain information Map collection has been stored.


Some questions in Section 2 of the solution
    
    method developer mentioned in Section 1 of the frequently used, there will still be some restrictions. For example: when a property of an object are numeric and string 5 '5 ", to take the two values are the same; when the attributes of the objects are objects of the attribute value of the two objects are to be set the same, and so on. This shows, according to the original solutions still have some problems, and there will be some difficulty in terms of positioning and debug problems.


Section 3 ES6 collection of Set
    
    1. How to Create a Set collection?

  Call the new Set () can be.

    2. Call the add () method: You can add elements to the collection .

    3. access to the collection size property can be the number of elements in the collection currently get . E.g:

1         let set = new Set();
2             set.add(5);
3             set.add("5");
4         console.log( set.size ); //2


    4. Invoke has () method, can determine whether there is a value set in the Set . E.g:

1 let set = new Set();
2      set.add(5);
3      set.add("5");
4 
5 console.log( set.has(5) ); //true
6 console.log( set.has(6) ); //false


    The call delete () method, can be removed Set set to a certain element .

    6. calls the clear () method, you can remove all elements in the collection . For example:

1 let set = new Set();
2      set.add(5);
3      set.add("5");
4      set.delete(5);
5 console.log( set.has(5) ); //false
6 
7      set.clear();
8 console.log( set.has("5") ); //false
9 console.log( set.size ); //0


    7. Call forEach () method to iterate Set collection.
        forEach () is received by three parameters: value / key / ownerSet (original set Set). For example:

 1  let set = new Set( [1, 2] );
 2      set.forEach(function(value, key, ownerSet) {
 3           console.log( key + " " + value );
 4           console.log( ownerSet === Set );
 5      });
 6 
 7     //打印
 8     //1 1
 9     //true
10     //2 2
11     //true


    8. The the Set interchangeable set of arrays.
        1) Set the array into a set of: passing to a constructor array can Set;
        2) Set the array into: using Expand operator. For example:

1  let set = new Set( [1,2,3,3,3,4,5] ),
2      array = [...set];
3  console.log( array ); //[1,2,3,4,5]


    9. Weak collection of the Set
        1) reasons.
            Set as long as a reference set of examples exist, garbage collection mechanism can not release the memory space of the object, then Set type previously mentioned may be seen as a strong reference. Weak Weak references Set if the object is the only reference (Set weak reference set) in, and will release the corresponding memory is recovered.

        2) Weak the Set create, add, judge, delete method:

. 1 the let SET = new new WeakSet (),
 2       Key = {};
 . 3              
. 4       // add objects to the collection 
. 5       set.add (Key);
 . 6       the console.log (set.has (Key)); // to true 
. 7  
. 8       . SET Delete (Key);
 . 9       the console.log (set.has (Key)); // to false


            Note: WeakSet constructor does not accept any of the original value, otherwise it will throw an error.

        . 3) the Set, WeakSet differences:
            A) In Weak Set example, to add () pass non-object parameters being given; pass parameters to the non-object has (), delete (), it returns false.
            b) Weak Set iteration not set, can not be used for-of loop is not supported forEach () method.
            c) Weak Set collection does not expose any iterator (keys (), values () ).
            d) Weak Set collection does not support the size attribute.

            Note: If you only need to track object references, should use WeakSet collection instead of Set collection.

Section 4 ES6 in the Map collection
    Map type is an ordered list of a number of key-value pairs stored. It can be set () method adds a new element to the Map set; Map to obtain information from the collection by get () method. In the object, the object can not be used as a key attribute of the object name, but in the Map collection can do so.

    1. the Map collection method supports.
        · Has (key) // detect whether there is a specified key
        · delete (key) // Removes the specified key name and the corresponding value
        · clear () // remove all keys on Map
        
    2. The initialization method Map collection
        Map constructor to initialize the array passed a Map collection. Each element of the array is a sub-array, the sub-array contains keys and key values ​​of two elements of a pair. For example:

1 let map = new Map( [["name", "zxx"], ["age", "25"]] );
2 
3 console.log( map.has("name") ); // true
4 console.log( map.get("name") ); // "zxx"
5 console.log( map.has("age") ); // true
6 console.log( map.get("age") ); // 25
7 console.log( map.size ); // 2


    3. forEach the Map set () method.
        Set and similar, which will not introduce the use of space.

    4. Weak collection of the Map
        · Weak Map collection of the keys must be an object, if you use a non-object key name error.
       · Weak Map is an unordered list.
        · Weak Map does not support the forEach () method, size and attributes clear () method.

 

(End of this section)

Guess you like

Origin www.cnblogs.com/zxxsteven/p/11479583.html