Javascript 如何创建公共和私人成员?

Javascript 如何创建公共和私人成员?,javascript,jquery,Javascript,Jquery,我有点困惑,如何创建公共和私人成员 到目前为止,我的代码模板如下所示: (function()){ var _blah = 1; someFunction = function() { alert(_blah); }; someOtherFunction = function() { someFunction(); } }(); 你不知道,将\uu前置到您的私有属性,然后不要使用不应该使用它的代码来接触它们 或您可以使用无法从外部访问的功能

我有点困惑,如何创建公共和私人成员

到目前为止,我的代码模板如下所示:

(function()){
   var _blah = 1;

   someFunction = function() {
     alert(_blah);
   };

   someOtherFunction = function() {
     someFunction();
   }
}();
你不知道,将
\uu
前置到您的私有属性,然后不要使用不应该使用它的代码来接触它们

您可以使用无法从外部访问的功能。

您可能需要使用:

您可以在定义了
myPrivateVar
myPrivateMethod
的地方定义您的私有成员,在定义了
myPublicProperty
myPublicMethod
的地方定义您的公共成员

您只需访问公共方法和属性,如下所示:

myModule.myPublicMethod();    // Works
myModule.myPublicProperty;    // Works
myModule.myPrivateMethod();   // Doesn't work - private
myModule.myPrivateVar;        // Doesn't work - private

Javascript没有真正的私有成员。如果您真的需要隐私,您必须滥用作用域。

在javascript中,每个对象成员都是公共的。声明私有字段最常用的方法是在其名称中使用下划线符号,只是为了让其他人知道这是私有字段:

a = {}
a._privateStuff = "foo"
隐藏变量的另一种方法是使用javascript中的作用域:

function MyFoo {
    var privateVar = 123;
    this.getPrivateVar = function() {
        return privateVar;
    }
}

var foo = new MyFoo();

foo.privateVar // Not available! 
foo.getPrivateVar() // Returns the value
下面这篇文章详细解释了这项技术:


我希望此URL能解决您的问题…请看


三件事:

  • IIFEs:
    似乎您需要更新有关此模式的知识。在再次使用iLife之前,先看一下post

  • 全球公众vs.安全公众:
    使用
    someFunction
    someOtherFunction
    跳过
    var
    关键字会导致这些函数对象在全局范围内注册,即可以从代码中的任何位置调用和重新分配这些函数。随着代码越来越大,这可能会导致一些严重的错误。相反,正如建议的那样,使用模块模式。虽然您也可以使用其他方法,如构造函数、Object.create()等,但这是非常容易实现的

    /* create a module which returns an object containing methods to expose. */
     var someModule = (function() {  
         var _blah = 1;  
          return {  
            someFunction: function() {},  
            someOtherFunction: function() {}  
          };  
     })(); 
    /* access someFunction through someModule */
    someModule.someFunction();
    // ...
    // ....
    /* after 500+ lines in this IIFE */
    /* what if this happens */  
    someFunction = function() {  
        console.log("Global variables rock.");  
    };  
    /* Fortunately, this does not interfere with someModule.someFunction */
    
  • 惯例和范围的组合:
    我们不能期望每个开发人员都遵循下划线或大写惯例。如果他们中的一个忘记了实现这个技术呢。我们可以将两者结合起来,而不仅仅依赖于约定(
    \u private
    GLOBAL
    )和范围(函数作用域)。这有助于保持编码风格的一致性,并提供适当的成员安全性。因此,如果下一次有人忘记大写他们的global,控制台(在严格模式下)可以防止世界末日


  • 第二个函数关键字后缺少
    ()
    。应该是
    someOtherFunction=function()
    。我个人喜欢将所有函数(公共函数和私有函数)定义为变量,然后将公共函数公开为myPublicMethod:myPublicMethod。
    /* create a module which returns an object containing methods to expose. */
     var someModule = (function() {  
         var _blah = 1;  
          return {  
            someFunction: function() {},  
            someOtherFunction: function() {}  
          };  
     })(); 
    /* access someFunction through someModule */
    someModule.someFunction();
    // ...
    // ....
    /* after 500+ lines in this IIFE */
    /* what if this happens */  
    someFunction = function() {  
        console.log("Global variables rock.");  
    };  
    /* Fortunately, this does not interfere with someModule.someFunction */