關(guān)于JS的閉包和公有私有變量屬性研究

認(rèn)識在js中的公有和私有變量

function car(){ 
    var wheel = 3;//私有變量 
    this.wheel = 4;//公有變量 
    alert(wheel); 
    alert(this.wheel); 
} 

這里創(chuàng)建的都是私有變量 sum num1 num2

function sum(num1, num2) {
        var sum = num1 + num2;
        return sum;
    }

我們怎么才能訪問到函數(shù)內(nèi)部的私有變量,怎么才能修改函數(shù)內(nèi)部的私有變量。
下面我簡單說下關(guān)于函數(shù)內(nèi)部私有和公有變量的一些處理辦法。
如果在這個(gè)函數(shù)內(nèi)部創(chuàng)建一個(gè)閉包,那么閉包通過自己的作用域鏈也可以訪問這些變量,利用這一點(diǎn),就可以創(chuàng)建用于訪問私有變量的公用方法。

我們把有權(quán)訪問私有變量和私有函數(shù)的公有方法叫做特權(quán)方法。

  • 1.構(gòu)造函數(shù)定義法
function myObject() {
        //私有屬性和私有函數(shù)
        var privateVariable = 10;
        function privateFunction() {
            return privateVariable;
        }
        //特權(quán)方法
        this.publicMethod = function() {
            privateVariable++;
            return privateFunction();
        }
    }
var object1 = new myObject();
    //訪問了函數(shù)內(nèi)部的局部變量即私有變量
console.log(object1.publicMethod());
function Person(name) {
        this.getName = function() {
            return name;
        };
        this.setName = function(value) {
            name = value;
        };
    }
    //創(chuàng)建對象實(shí)例
    var person = new Person("李雷");
    console.log(person.getName());
    person.setName("韓梅梅");
    console.log(person.getName());

在以上構(gòu)造函數(shù)中定義了兩個(gè)特權(quán)方法:getName()和setName()。這兩個(gè)方法都可以在函數(shù)外部使用,而且都有權(quán)訪問私有變量name。

  • 2.靜態(tài)私有變量
    通過在私有作用域中定義私有變量或函數(shù),同樣也可以創(chuàng)建特權(quán)方法
(function() {
        //私有變量和私有函數(shù)
        var privateVariable = 10;
        function privareFunction() {
            return false;
        };
        //構(gòu)造函數(shù)
        MyObject = function() {}
        //公有、特權(quán)方法
        MyObject.prototype.publicMethod = function() {
            privateVariable++;
            privareFunction();
        }
    })();
(function() {
        var name = "";
        Person = function(value) {
            name = value;
        };
        //添加原型方法
        Person.prototype.getName = function() {
            return name;
        };
        Person.prototype.setName = function(value) {
            name = value;
        };
    })();
    var person1 = new Person("lilei");
    console.log(person1.getName());
    person1.setName("hanmeimei");
    console.log(person1.getName());

    var person2 = new Person("mahuateng");
    console.log(person1.getName());
    console.log(person2.getName());
  • 3.模塊模式
    模塊模式是為單例創(chuàng)建私有變量和特權(quán)方法,單例就是指,只有一個(gè)實(shí)例的對象。
var singlton = {
        name: value,
        method: function() {
            //這是方法的代碼 
        }
    }
var singlton = function() {
        //私有變量和私有函數(shù)   
        var privateVaiable = 10;
        function privateMethod() {
            return false;
        };
        //公有/特權(quán)方法   
        return {
            publicProperty: true,
            piblicMethod: function() {
                privateVaiable++;
                return privateMethod();
            }
        }
    }();
function BaseComponent() {}

    function OtherComponent() {}
    var application = function() {
        //私有變量和函數(shù)
        var components = new Array();

        //初始化
        components.push(new BaseComponent());

        //公有
        return {
            getComponentCount: function() {
                return components.length;
            },

            registerComponent: function(component) {
                if(typeof component == "object") {
                    components.push(component);
                }
            }
        };
    }();

    application.registerComponent(new OtherComponent());
    alert(application.getComponentCount()); //2
  • 4.增強(qiáng)的模塊模式
var singlton = function(){

        //私有變量和私有函數(shù)   
        var privateVaiable = 10;

        function privateMethod() {
            return false;
        };
        //創(chuàng)建對象 
        var object = new CustomType();

        //公有/特權(quán)方法   
        object.publicProperty = true;

        object.piblicMethod = function() {
            privateVaiable++;
            return privateMethod();
        };
        //返回這個(gè)對象 
        return object;
    }();
function BaseComponent() {}

    function OtherComponent() {}

    var application = function() {

        //私有變量和私有函數(shù)   
        var components = new Array();

        //創(chuàng)建對象 
        components.push(new BaseComponent());

        //創(chuàng)建application的一個(gè)局部副本
        var app = new BaseComponent();

        //公共
        app.getComponentCount = function() {
            return components.length;
        };

        app.registerComponent = function(component) {
            if(typeof component == "object") {
                components.push(component);
            }
        };

        //返回這個(gè)副本
        return app;
    }();

    alert(application instanceof BaseComponent);
    application.registerComponent(new OtherComponent());
    alert(application.getComponentCount()); //2
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近學(xué)這塊知識學(xué)得有些吃力。還有很多遺漏的地方,只能以后多看些書來彌補(bǔ)了。 第7章 函數(shù)表達(dá)式 函數(shù)定義的兩種方式...
    丨ouo丨閱讀 470評論 0 1
  • JavaScript 中沒有塊級作用域的概念 因?yàn)闆]有塊級作用域,所以該函數(shù)可以直接運(yùn)行,而不報(bào)錯(cuò)。 這里有個(gè)比較...
    TaoGeNet閱讀 426評論 0 0
  • 定義函數(shù)的方式有兩種:函數(shù)聲明和函數(shù)表達(dá)式。 函數(shù)聲明的一個(gè)重要特征就是函數(shù)聲明提升,意思是在執(zhí)行代碼前會(huì)先讀取函...
    oWSQo閱讀 731評論 0 0
  • 細(xì)雨霏霏。 風(fēng)把簾子吹的鼓鼓的,米黃色的亞麻簾,上面繡著兩棵向日葵,魯繡,淡雅別致,簡單隨性。天空烏云密布,室內(nèi)光...
    淡淡翠閱讀 463評論 5 6
  • 請不要一次再一次的拿出自己的臆想侮辱別人 也請不要隨意臆測別人 更不要看到什么類似的就一通報(bào)復(fù) 也許,這是埋葬
    起不出名字的小檗閱讀 481評論 0 1

友情鏈接更多精彩內(nèi)容