简体   繁体   English

模块模式中的私有变量

[英]Private Variables in Module Pattern

I am not clear about the concept of private, if I can still access it through the public method and redefine the properties of the module. 如果我仍然可以通过public方法访问它并重新定义模块的属性,则我不清楚private的概念。 I mean, I can perfectly do: 我的意思是,我可以完美地做到:

var aModule = (function() {

    var privateVar = 1;

    return {
        publicFunction: function() {
            return privateVar;
        }
    }

})();

aModule.publicFunction = function() {
    privateVar = function() {
        console.log('a new content');
    }
    privateVar();
};

aModule.publicFunction(); // 'a new content'

I understand that this is not possible if I write it in ES6 with let or const, because it would give me error try to overwrite the value of the private variable, but what sense does it have in ES5? 我知道如果我用let或const在ES6中编写它是不可能的,因为尝试覆盖私有变量的值会给我带来错误,但是在ES5中它有什么意义?

A private variable cannot be accessed or changed by code that's outside the module or class that owns the private variable. 私有变量不能由拥有私有变量的模块或类之外的代码访问或更改。

For example, you can't do aModule.privateVar and expect it to give you anything back. 例如,您不能执行aModule.privateVar并期望它能给您任何回报。

What your publicFunction is is what the Java (and other programming languages) world would call a "getter". 您的publicFunction是Java(和其他编程语言)界称为“ getter”的东西。 Simply put it gives access to the value of the private variable, without allowing write access to it. 简单地说,它允许访问私有变量的值,而不允许对其进行写访问。

In your last example, you're not actually overwriting the private variable. 在最后一个示例中,您实际上并没有覆盖私有变量。 You're just creating a new variable within publicFunction 's scope and assigning a value to that. 您只是在publicFunction的范围内创建一个新变量, publicFunction分配值。 Just because it's also named privateVar doesn't mean it's the same area of memory. 仅仅因为它也被命名为privateVar并不意味着它是相同的内存区域。

I've added to your code to demonstrate this 我已添加到您的代码中以演示这一点

 var aModule = (function() { var privateVar = 1; return { publicFunction: function() { return privateVar; }, getPrivateVar() { return privateVar; } } })(); aModule.publicFunction = function() { privateVar = function() { console.log('a new content'); } privateVar(); }; aModule.publicFunction(); // 'a new content' console.log(aModule.getPrivateVar()); //outputs 1 

To give more detail on why this is, it's all about scope. 要详细说明原因,这全都与范围有关。 privateVar exists in an anonymous function's scope. privateVar存在于匿名函数的作用域中。 This anonymous function returns an object with several functions defined on it. 这个匿名函数返回一个对象,上面定义了几个函数。 When the function is called, this object is assigned to aModule, but retains access to privateVar because they share scope. 调用该函数时,该对象被分配给aModule,但保留对privateVar的访问权限,因为它们共享范围。

However, outside of that function we're at a different scope, which doesn't have access to the aModule's variables, except those exposed in the returned object 但是,在该函数之外,我们处于不同的范围,该范围无权访问aModule的变量,除了在返回的对象中公开的变量外

You are overwriting the public function and not able to access the module private variable 您正在覆盖public函数,但无法访问模块私有变量

Consider the following new function that is created as a property of aModule that attempts to only change the value of var privateVar 考虑以下作为aModule属性创建的新函数,该函数试图仅更改var privateVar的值

The scope is different because of where it gets called and it isn't able to access that private variable 范围因调用位置而不同,因此无法访问该私有变量

 var aModule = (function() { var privateVar = 1; return { publicFunction: function() { return privateVar; } } })(); aModule.newFunction = function() { // try to change privateVar in aModule privateVar = 3 }; aModule.newFunction(); console.log(aModule.publicFunction()); //still 1 not 3 // here's where it actually ended up console.log(window.privateVar) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM