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>
发布了64 篇原创文章 · 获赞 7 · 访问量 6689

猜你喜欢

转载自blog.csdn.net/MENGCHIXIANZI/article/details/105354106
今日推荐