js學習之ES6入門基礎篇_補充_Get和Set

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script type="text/javascript">
       
        // 原型鏈的問題
        json = {
            a : function(){
                console.log('普通');
            },
            b(){
                console.log('沒有冒號啊,大丈夫');
            },
            c : () =>{
                console.log('箭頭函數啊,大丈夫');
            }
        }

        // 曉調一下
        json.a();
        json.b();
        json.c();

        // Get Set
        jsonSG = {
            set setF(x){
                console.log("這是Set   "+x);
            },
            get getF(){
                console.log("這是Get   ");
            }

            // ,getF:1 // 這個也是錯的,不能有這種二義性的元素
            // 會識別不出來,只能返回 undefined 
            ,get getF1(){
                return{
                    get getF11(){
                        console.log("這是Get   裏面的Get");
                    }
                }
            }
        }
        
        // 調用
        // 如果不賦值 就會調用 get方法
        jsonSG.getF;
        // jsonSG.setF('123456');   // 這是錯誤的調用方法
        // 如果賦值了,就會調用set方法
        // 需要注意的是:
        // 這個set只能賦值一個參數
        // 並不能賦值複數個參數
        jsonSG.setF = '1234567890' ;
        // 調用
        jsonSG.getF1.getF11;

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章