我有一个问题如何将 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();
如何添加命名空间?非常感谢您的帮助。
最佳答案
按照你说的,改成
mynamespace1.mynamespace2.MyAnotherclass = function(param1) {
...
};
关于javascript - 如何将 google Closure 命名空间添加到 Javascript 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28329861/