搜索
您的当前位置:首页正文

JavaScript函数_07 私有变量 + 私有函数 + 特权

来源:二三娱乐

私有变量

使用 var 关键字声明在函数内部的变量称为私有变量

私有函数

在函数内部声明的函数称为私有函数

特权方法

可以访问构造函数内部的私有变量和函数,这种方法被称为特权方法

<script>
    function Person(){
        this.name = "momo";
        this.showName = function(){};

        var a = "测试的值";
        function getA(){
            return a;
        };
        //像test这样的实例方法,它可以访问构造函数内部的变量和函数,这种方法被称为特权方法
        this.test = function(){
            console.log(getA());
        };

    }

    var p1 = new Person();
    p1.test();

</script>
Top