• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • new Set()

    Set构造函数能让你创建Set对象,其可以存储任意类型的唯一值,无论是基本类型或者对象引用。


    语法

    new Set()
    new Set(iterable)
    

    备注:Set()只能用new构建。试图在没有new的情况下调用它,会抛出TypeError


    参数

    iterable可选

    如果传递一个可迭代对象,它的所有元素将不重复地被添加到新的Set中。如果不指定此参数或其值为null,则新的Set为空。


    返回值

    一个新的Set对象。


    示例

    使用Set对象

    const mySet = new Set();
    
    mySet.add(1); // Set [ 1 ]
    mySet.add(5); // Set [ 1, 5 ]
    mySet.add(5); // Set [ 1, 5 ]
    mySet.add('some text'); // Set [ 1, 5, 'some text' ]
    const o = { a: 1, b: 2 };
    mySet.add(o);
    

    上篇:Set

    下篇:get Set[@@species]