[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Guide/Keyed_collections [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Set Map - .

In this article

Map

Map - (). /( ) .

,

  • ,
  • / /
  • ( ).

, , :

:

js
var sayings = new Map();
sayings.set("dog", "woof");
sayings.set("cat", "meow").set("elephant", "toot");
//  .set  Map,  set    

sayings.set("dog", "-"); //    

sayings.size; // 3
sayings.get("fox"); // undefined
sayings.has("bird"); // false
sayings.delete("dog");

for (var [key, value] of sayings) {
  console.log(key + " goes " + value);
}
// "cat goes meow"
// "elephant goes toot"

Map .

WeakMap

WeakMap , , WeakMap ( (garbage collector GC)).

: WeakMap Map, - WeakMap (.e. ). , garbage collection.

, , " WeakMap?" WeakMap.

, WeakMap, , . Hiding Implementation Details with ECMAScript 6 WeakMaps (.). privates . ; :

js
const privates = new WeakMap();

export class Public() {

  constructor() {
    const me = {
    //    
    };
    // 'me'     'this' !!!
    privates.set(this, me);
  }

  method () {
    const me = privates.get(this);
    //  -     'me'...
  }
}

Set

Set - , .

Set:

js
var mySet = new Set();
mySet.add(1);
mySet.add("some text");
mySet.add("foo");

mySet.has(1); // true
mySet.delete("foo");
mySet.size; // 2

for (let item of mySet) console.log(item);
// 1
// "some text"

Set

Array Set

Array Set Array.from spread operator.

, Set Array .

: Set , Array .

js
Array.from(mySet);
[...mySet2];

mySet2 = new Set([1, 2, 3, 4]);

Array Set

, , :

WeakSet

WeakSet , . WeakSet ( (garbage collector GC)).

: WeakSet , Set.

Set:

  • WeakSet ( ).
  • WeakSet . () .

WeakSet . , , , , DOM-.

WeakSet

Map Set

Map objects Set "same-value-zero algorithm":

  • ===.
  • -0 +0 .
  • NaN ( ===).

Web Proxy Viewer  |  New URL  |  Original Page