ES6中的const,set,map,Symbol

const,常量聲明者

  1. 01-只在當前的代碼塊有效
  2. 02-做用域不會被提高
  3. 03-在一個做用域裏不能重複聲明
  4. 04-申明的常量必須當即賦值
  5. 05能夠改變堆區的對象內容,不能修改棧區的地址(和其餘語言如Java相似)
    這裏寫圖片描述
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<script>
/*    //01-只在當前的代碼塊有效
    {
        const a = "zhangsan"
    }
    console.log(a);//報錯*/
    //02-做用域不會被提高
    //03-在一個做用域裏不能重複聲明
/*    //04-申明的常量必須當即賦值
{
    const name;
    name = "zhangsan";
    console.log(name);
}*/
    {
        const obj = {name : "張三"};
        console.log(obj);

        obj.name = "李四";
        console.log(obj);
    }

</script>
</body>
</html>

set,無重集合

<!doctype html>
<html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>

    <script>
        //1.建立集合
        let set = new Set(['張三','李四','李四']);
        console.log(set);

        //2.屬性
        console.log(set.size);

        //03-四個方法 add delete has clear
        set.add('hehe').add('dada');
        console.log(set);
        console.log(set.delete('dada'));
        console.log(set);

        console.log(set.has('張三'));
        console.log(set.has('張三1'));

        set.clear();//這個就返回值了
        console.log(set);

    </script>
    </body>
    </html>

map,鍵值對,彌補對象的侷限性

<!doctype html>
<html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>

    <script>
/*        //對象的侷限性
        let obj1 = {a:1},obj2 = {b:2},obj = {};
        console.log(obj);
        obj.name = '張三';
        obj[obj1] ="天空";
        console.log(obj);
        obj[obj2] = "大海"
        console.log(obj);//被覆蓋

        //緣由就是內部把鍵作了字符串處理化
        console.log(obj1.toString());
        console.log(obj2.toString());
        console.log(obj1.toString() === obj2.toString());//true*/

        //引入Map

        //1.建立一個Map
        let obj1 = {a:1},obj2 = {b:2},obj = {};
        const  map = new Map([
            ['name','張三'],
            ['age','18'],
            ['sex','男'],
            [obj1,'今每天氣很好'],
            [obj2,'適合敲代碼'],
            ['age','50'],//覆蓋前面的18
            [[1,2],'hh'],
        ]);
        console.log(map);
        console.log(map.size); //6 一樣的鍵會被覆蓋

        //set和get
        //set新增長鍵值對
        map.set('friends',['趙六','王五']).set(['dog'],'小花');
        //set獲取
        console.log(map.get('name'));
        console.log(map.get(obj1));

        //delete ,經過鍵刪除對
        map.delete(obj1)
        console.log(map);

        //has 是否存在該鍵
        console.log(map.has(obj1)); //false
        console.log(map.has(obj2)); //true

        //clear 清空

        //keys,values,entries
        console.log(map.keys());
        console.log(map.values());
        console.log(map.entries());//獲取鍵值對

        //遍歷
        map.forEach(function (value,key) {
            console.log(key + ":" + value);
        })

        //注意事項,對象都爲空時,不會被覆蓋,由於在堆區新開的對象地址都是不同的
        map.set({},'hahaha');
        map.set({},'hehehehe');
        console.log(map);
        console.log({} === {});//false

    </script>
    </body>
    </html>

Symbol,解決字符串命名衝突問題

這裏寫圖片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06-Symbol</title>
</head>
<body>
<script>
/*    //1.定義
    let str1 = Symbol();
    let str2 = Symbol();
    console.log(str1 === str2);
    console.log(typeof str1);
    console.log(typeof str2);*/

/*    //2.描述
    let str3 = Symbol("name");
    let str4 = Symbol("name");
    console.log(str3);
    console.log(str4);
    console.log(str3 === str4);*/

    //3.對象的屬性名
    const obj = {};
    obj.name = 'zhangsan';
    obj.name = 'lisi';
    obj[Symbol('name')] = 'zhangsan';
    obj[Symbol('name')] = 'lisi';
    console.log(obj);
</script>
</body>
</html>
相關文章
相關標籤/搜索