如何在公开 public 静态方法的同时 Javascript 中的 'hide' 变量?

How do I 'hide' variables in Javascript whilst exposing public static methods?

我目前正在使用 Javascript 重写一些科学 C++ 代码,如果可能的话,我希望保持相同的基本组织。在 C++ 代码中,有许多 类 包含许多不同数组中的一堆 const 数据,以及一系列处理该数据的 public static 方法。我正在努力研究如何在 Javascript.

中复制这样的东西

目前我正在使用如下内容:

function mars(){}

mars.x = [{A:1, B:2, C:3},{A:1, B:2, C:3}]; //...
mars.y = [{A:1, B:4, C:2},{A:1, B:2, C:3}]; //...
// ...about 600 lines in total

mars.doSomething = function(foo){
    var result = 0;
    // Do lots of processing of above arrays
    return result;
}

mars.doSomethingElse = function(bar){
    var result = 0;
    // Do lots of processing of above arrays
    return result;
}

console.log(mars.doSomething(3))

这有效,但它会将 mars.x 等暴露给实际上不需要了解它的其余代码。如果我使用 prototype,方法将不再是静态的,代码中将充斥着 new 调用,这是我真正不想要的。

那么我要问的是:如何在 JavaScript 中隐藏变量,同时将静态方法暴露给其余代码?还是我在担心一些我不应该担心的事情?

只需将其设为局部变量并使用 getter 函数来获取它们。另外,请尝试维护命名方案。对于构造函数,就是名字要以大写字母开头。

function Mars(){
   var x = [{A:1, B:2, C:3},{A:1, B:2, C:3}];
   this.getArr = function(){
       return x;
   }
}
var mars = new Mars();
mars.x; // undefined
mars.getArr(); // [Object, Object]

要隐藏变量,可以使用闭包(函数作用域)

function mars () {
  var staticFunctions = Object.create(null); // the same as {}
  var x = [{A:1, B:2, C:3},{A:1, B:2, C:3}];
  var y = [{A:1, B:4, C:2},{A:1, B:2, C:3}];

  staticFunctions.doSomething = function (foo) {
    return x;
  };

  staticFunctions.doSomethingElse = function (bar) {
    return y;
  };

  return staticFunctions;
}

// you do need to instantiate mars however
var m = mars();
// if you don't want that you can do
var mars = (function () {
    // same as above
} ()); // this invokes the function for you

你担心这个是正确的。如果你不解决这个问题,你的代码将变得难以管理。

您使用函数来控制 JavaScript 中的范围。 JS 没有 类。 ES6(未广泛使用)有 "classes",但名副其实。

您可以尝试这样的操作:

var myApp = {};

(function(namespace){

  var X = [{A:1, B:2, C:3},{A:1, B:2, C:3}]; // Capitalized because 'constant'.
  var Y = [{A:1, B:4, C:2},{A:1, B:2, C:3}]; // Capitalized because 'constant'.

  function Mars () {} // Capitalized because this is a constructor function.

  Mars.prototype.doSomething = function(foo) {
    //do something with X
    return X[foo]; // For example.
  }

  namespace.mars = new Mars(); // Lowercase on 'namespace.mars' because it is an instance.

}(myApp))


// Use...
myApp.mars.doSomething(foo); // Does something.
Mars; // undefined
X; // undefined
Y; //undefined

我认为下面的示例可以让您深入了解私有、public 和静态变量和方法。

function Mars(){
  // private variables
  var x = [{A:1, B:2, C:3},{A:1, B:2, C:3}];
  var y = [{A:1, B:4, C:2},{A:1, B:2, C:3}]; 

  // public variables
  this.z = 2;

  // privileged methods, can access private variables
  this.getX = function(){
      return x;
  };

  this.getY = function(){
      return y;
  };  
}

// public prototype method, can not access private variables
// access private variables through getters
Mars.prototype.getZ = function(){
  return this.z;
}

// static variable
Mars.staticVar = 1;

// static method
Mars.staticMethod = function(){
   console.log('It is the static method');
}



var marsObj = new Mars();
console.log(marsObj.getX()); //Array [ Object, Object ]
console.log(marsObj.getZ()); //2

Mars.staticMethod(); // "It is the static method"
marsObj.staticMethod(); // Exception: mars.staticMethod is not a function

要了解 JavaScript 中的 OOPS,我推荐此教程: http://phrogz.net/JS/classes/OOPinJS.html

我看到您想提供一些结构并组织您的 javascript 代码。这最好在 Javascript 中用众所周知的 Module Pattern

来处理

简而言之,它是这样工作的:

var MODULE = (function () {
    var my = {},
        privateVariable = 1;


    function privateMethod() {
        // ...
    }

    my.moduleProperty = 1;
    my.moduleMethod = function () {
        // ...
    };

    return my;
}());

此代码段和进一步阅读记录在 Ben Cherry's article and Eric Miraglia's article. There are some neat variations of the module pattern, one such is the revealing module pattern by Christian Heilmann