第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從原型定義函數(shù)訪問私有成員變量

從原型定義函數(shù)訪問私有成員變量

牛魔王的故事 2019-07-11 15:44:08
從原型定義函數(shù)訪問私有成員變量是否有任何方法使“私有”變量(那些在構(gòu)造函數(shù)中定義的變量)可用于原型定義的方法?TestClass = function(){     var privateField = "hello";     this.nonProtoHello = function(){alert(privateField)};};TestClass.prototype.prototypeHello = function(){alert(privateField)};這樣做是可行的:t.nonProtoHello()但這并不是:t.prototypeHello()我習(xí)慣于在構(gòu)造函數(shù)中定義我的方法,但出于以下幾個原因,我不這么做了。
查看完整描述

3 回答

?
慕蓋茨4494581

TA貢獻1850條經(jīng)驗 獲得超11個贊

當(dāng)我讀到這篇文章時,這聽起來像是一個艱巨的挑戰(zhàn),所以我決定想出一個辦法。我想出的是瘋狂但它完全有效。


首先,我嘗試在直接函數(shù)中定義類,這樣您就可以訪問該函數(shù)的一些私有屬性。這可以讓您獲得一些私有數(shù)據(jù),但是,如果您試圖設(shè)置私有數(shù)據(jù),您很快就會發(fā)現(xiàn)所有對象都將共享相同的值。


var SharedPrivateClass = (function() { // use immediate function

    // our private data

    var private = "Default";


    // create the constructor

    function SharedPrivateClass() {}


    // add to the prototype

    SharedPrivateClass.prototype.getPrivate = function() {

        // It has access to private vars from the immediate function!

        return private;

    };


    SharedPrivateClass.prototype.setPrivate = function(value) {

        private = value;

    };


    return SharedPrivateClass;

})();


var a = new SharedPrivateClass();

console.log("a:", a.getPrivate()); // "a: Default"


var b = new SharedPrivateClass();

console.log("b:", b.getPrivate()); // "b: Default"


a.setPrivate("foo"); // a Sets private to "foo"

console.log("a:", a.getPrivate()); // "a: foo"

console.log("b:", b.getPrivate()); // oh no, b.getPrivate() is "foo"!


console.log(a.hasOwnProperty("getPrivate")); // false. belongs to the prototype

console.log(a.private); // undefined


// getPrivate() is only created once and instanceof still works

console.log(a.getPrivate === b.getPrivate);

console.log(a instanceof SharedPrivateClass);

console.log(b instanceof SharedPrivateClass);

有很多情況下,這將是足夠的,例如,如果您希望有常量的值,如事件名稱,并在實例之間共享。但本質(zhì)上,它們的作用就像私有的靜態(tài)變量。


如果您絕對需要從原型上定義的方法中訪問私有命名空間中的變量,則可以嘗試此模式。


var PrivateNamespaceClass = (function() { // immediate function

    var instance = 0, // counts the number of instances

        defaultName = "Default Name",  

        p = []; // an array of private objects


    // create the constructor

    function PrivateNamespaceClass() {

        // Increment the instance count and save it to the instance. 

        // This will become your key to your private space.

        this.i = instance++; 

        

        // Create a new object in the private space.

        p[this.i] = {};

        // Define properties or methods in the private space.

        p[this.i].name = defaultName;

        

        console.log("New instance " + this.i);        

    }


    PrivateNamespaceClass.prototype.getPrivateName = function() {

        // It has access to the private space and it's children!

        return p[this.i].name;

    };

    PrivateNamespaceClass.prototype.setPrivateName = function(value) {

        // Because you use the instance number assigned to the object (this.i)

        // as a key, the values set will not change in other instances.

        p[this.i].name = value;

        return "Set " + p[this.i].name;

    };


    return PrivateNamespaceClass;

})();


var a = new PrivateNamespaceClass();

console.log(a.getPrivateName()); // Default Name


var b = new PrivateNamespaceClass();

console.log(b.getPrivateName()); // Default Name


console.log(a.setPrivateName("A"));

console.log(b.setPrivateName("B"));

console.log(a.getPrivateName()); // A

console.log(b.getPrivateName()); // B


// private objects are not accessible outside the PrivateNamespaceClass function

console.log(a.p);


// the prototype functions are not re-created for each instance

// and instanceof still works

console.log(a.getPrivateName === b.getPrivateName);

console.log(a instanceof PrivateNamespaceClass);

console.log(b instanceof PrivateNamespaceClass);

我希望任何看到錯誤的人都能給我一些反饋。


查看完整回答
反對 回復(fù) 2019-07-11
  • 3 回答
  • 0 關(guān)注
  • 449 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號