如何将google Closure命名空间添加到Javascript类

时间:2015-02-04 19:34:22

标签: javascript google-closure

我有一个问题如何将google Closure命名空间添加到javascript类。这有效:

goog.provide('mynamespace1.mynamespace2.Myclass');
goog.require('goog.dom');

mynamespace1.mynamespace2.Myclass = function(param1) {
  this._param1 = param1;
  this.public_method();
  var private_method = function() {
  };
  private_method();  // Not really sure how to access it by public_method();
};

mynamespace1.mynamespace2.Myclass.prototype.public_method = function() {

};

// Test1
var test1 = new mynamespace1.mynamespace2.Myclass(1);
test1.public_method();

但是,如果javascript类定义是:

goog.provide('mynamespace1.mynamespace2.MyAnotherclass');
goog.require('goog.dom');

function MyAnotherclass(param1) {
  var that = this;
  this._param1 = param1;

  this.private_method = function() {
  };

  this.private_method();

  that.public_method = function() {
  };
};

// Test 2
var test2 = new MyAnotherclass(1);
test2.public_method();

如何添加命名空间?非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如你所说,将其改为

mynamespace1.mynamespace2.MyAnotherclass = function(param1) {
    ...
};
相关问题