JavaScript OOP私有变量 [英] JavaScript OOP private variables

查看:106
本文介绍了JavaScript OOP私有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这是对我以前的问题的更新,对于这个StackExchange的目的有点偏离主题。

Updated: This is an update to my previous question that was somewhat off topic as to what this StackExchange is aiming for. But I have a follow up question to the previous inquiry about this subject.

对象模型:

 var Soldier;

 Soldier = (function() {

    "use strict";

    function Soldier() {
        var privateVar = "privateValue";

        this.methodToGetPrivateValue = function() {
            return privateVar;
        }
    }

    var sharedPrivateVar = "sharedPrivateValue";

    function sharedPrivateMethod() {
        // I want to get value `privateVar`
    }

    Soldier.prototype = {
        publicVar: "publicValue",

        publicMethod: function() {
            return this.publicVar;
        },

        sharedPrivate: function() {
            return sharedPrivateVar;
        }
    }

    return Soldier;

 })();

 var marine = new Soldier();

所以我更新的问题,使这个话题更合适的问题是,如果有反正要获得sharedPrivateMethod以这种方式定义为能够访问上面设置中的私有变量?

So my updated question to make this topic more a proper question is if there is anyway to get a sharedPrivateMethod defined in this way to be able to access the private variable in the above setup?

我要求的原因是sharedPrivateMethod对于实例化对象是完全不可见的。而Soldier()中定义的函数对于实例是可访问的,因为this.method = function()。

The reason I am asking is that the sharedPrivateMethod is totaly invisible to the instanced object. While the function defined inside Soldier() is accessible to the instance because of the this.method = function(). I dont know if it has any real use at the moment but would be interesting to see if it was possible somehow.

推荐答案

我不知道现在是否有任何真正的用途,你所拥有的问题是你的 _self 变量是由通过 new Test 实例共享的$ c>,例如,假设您的 privateMethod 使用它:

The problem with what you have there is that your _self variable is shared by all instances constructed via new Test, and so for instance, assume your privateMethod used it:

function privateMethod() {
    console.log(_self.message);
}

然后:

var t1 = new Test();
t1.message = "Message 1";
var t2 = new Test();
t2.message = "Message 2";

t1.privateMethod();

...会记录Message 2,而不是Message 1第二次调用 new Test 已覆盖 _self _ 变量。

...would log "Message 2", not "Message 1" as you would expect, because the second call to new Test has overwritten the _self_ variable.

除了 _self 变量,你有很好。它允许你有所有实例共享的私有数据和函数,这是非常方便。如果你需要真正的每个实例的私有数据,你需要创建一个函数,在构造函数本身使用这些数据:

Other than the _self variable, what you have is fine. It lets you have private data and functions shared by all instances, which is very handy. If you need to have truly private data that's specific to each instance, you need to create the function that uses that data in the constructor function itself:

function Test() {
    var trulyPrivate = 42;

    this.showTrulyPrivate = function() {
        console.log("trulyPrivate = " + trulyPrivate);
    };
}

然后 truePrivate 真正私人的实例。成本是为每个实例创建 showTrulyPrivate 函数的成本。 (函数对象可能能够共享底层代码,一个好的引擎会这样做,但是单独的函数对象。)

Then trulyPrivate is genuinely private to the instance. The cost is the cost of creating a showTrulyPrivate function for each instance. (The function objects may be able to share the underlying code, a good engine will do that, but there will be separate function objects.)

所以包装:

var Test = (function() {
    // Data and functions defined here are private to this code
    // and shared across all instances. There is only one copy
    // of these variables and functions.
    var privateDataSharedByAll;

    function privateFunctionSharedByAll() {
    }

    function Test() {
        // Data and functions here are created for *each* instance
        // and are private to the instance.
        var trulyPrivate;

        this.hasAccessToTrulyPrivate = function() {
            // Has access to the shared private data *and* the
            // per-instance private data.
        };
    }

    return Test;
})();

这篇关于JavaScript OOP私有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆