JS高級-day05-Set對象及面向對象小解

一, Set對象

介紹

  1. 是一個對象

  2. 特點 : 存放的數據不會重復

  3. 數組轉成 set 對象

    const set = new Set([1, 2, 3, 4])
    
  1. set對象轉化成數組

    const arr = [...set]
    
  1. set對象存在一個方法 add

    set.add(13)
    

代碼

    <script>
        /*
        1. Set 是一個對象 存放數據 數據永遠不會重復
        2. Set 當成是一個數組
        3. Set 是一個對象 遍歷 使用 數組方法 find findindex map
            把Set對象轉成 真正的數組
            數組轉成 set 對象2
            const set = new Set ([1, 2, 3])

        4 小結
            1 Set 是一個對象 不會存放重復數據
            2 數組轉成 set對象  const set =   new Set([])
            3 set對象 轉成 數組  const arr=[...set]
            4 set對象 添加數據 使用add方法
            set.add(1)
            set.add(2)
            set.add(3)        
        */
       
        // 存在舊數組
        const list = [1, 2, 6, 7, 8]

        // 1. Set 對象 需要被 new 出來使用
        const set = new Set(list)

        // 2. 存放數據 調用 add方法
        set.add(1)
        set.add(2)
        set.add(3)
        set.add(3)
        set.add(3)
        set.add(4)
        set.add(5)
        
        // console.log(set);
        // 把set對象 轉成數組
        let arr = [...set]
        console.log(arr);
    </script>

二, 面向對象

1. 概念: 項目級別代碼思維

2. 特征: 封裝和繼承

3. 創建對象的三種方式

1. 字面量

大量類似對象想要修改, 不方便后期維護
    <script>
        // 1 創建對象的方式  字面量 => 字面意思  (你是個好人)
        // 不方便維護 - 修改
        // const obj = { nickname: '八戒', height: 190 };
        // const obj1 = { nickname: '八戒', height: 190 };
        // const obj2 = { nickname: '八戒', height: 190 };
        // const obj3 = { nickname: '八戒', height: 190 };
        // const obj4 = { nickname: '八戒', height: 190 };
        // const obj5 = { username: '八戒', height: 190 };   
      </script>

2. 工廠函數

  1. 解決不方便維護對象的弊端

  2. 但是沒有辦法去實現 '繼承'

            // 語法
            function Person(name,height){
             return { name,height}
            }
    
        <script>
            // 2 工廠函數 封裝 繼承!  
            function p(name,a,b,c,d,e) {
              return {
                nickname:name,
                a,b,c,d,e
              }
              
            }
            function person(name, height,a,b,c,d,e) {
              return {
                nickname: name,
                height,
                a,b,c,d,e
              };
            }
      
            const obj = person('八戒', 190);
            const obj1 = person('八戒', 190);
            const obj2 = person('八戒', 190);
            const obj3 = person('八戒', 190);
            console.log(obj);
            console.log(obj1);
            console.log(obj2);
            console.log(obj3);  
          </script>
    

3. 構造函數!!以下重點講

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容