Google Closure中的'命令'是否有原型?

时间:2016-05-19 18:41:10

标签: javascript google-closure-compiler google-closure

我正在使用Google Closure并尝试执行以下操作:

abc.edf.commands.showToast = function() {
    abc.edf.commands.showToast.test(); // this works fine
    this.test2(); // this will throw an error like:   
    // exception_logger.js:88 TypeError: this.test2 is not a function
};

abc.edf.commands.showToast.test = function() {
    console.log('test');
};

abc.edf.commands.showToast.prototype.test2 = function() {
    console.log('test2');
};

我认为JavaScript中的每个对象都有'原型'。那是因为'命令'不是一个对象吗?或者我错过了别的什么?谢谢:))

1 个答案:

答案 0 :(得分:2)

正如Felix Kling评论的那样,您需要使用/* Example usage #1 */ let ex1_arr1 : [Any?] = ["foo", nil, 3, "bar"] let ex1_arr2 : [Any?] = ["foo", nil, 3, "bar"] compareAnyArrays(ex1_arr1, ex1_arr2) // true /* Example usage #2 */ let ex2_arr1 : [Any?] = ["foo", nil, 2, "bar"] let ex2_arr2 : [Any?] = ["foo", 3, 2, "bar"] compareAnyArrays(ex2_arr1, ex2_arr2) // false 创建一个对象才能使用new原型函数。这是一个例子:

test2

您可以尝试在online closure compiler中输入该代码。以下是使用简单编译编译的内容:

goog.provide('abc.edf.commands.showToast');

/** @constructor */
abc.edf.commands.showToast = function() {};

abc.edf.commands.showToast.test = function() {
    console.log('test');
};

abc.edf.commands.showToast.prototype.test2 = function() {
    console.log('test2');
};

abc.edf.commands.showToast.test(); // static class method
var foo = new abc.edf.commands.showToast();
foo.test2(); // instance method

以下是使用高级编译编译的内容:

var abc={edf:{}};
abc.edf.commands={};
abc.edf.commands.showToast=function(){};
abc.edf.commands.showToast.test=function(){console.log("test")};
abc.edf.commands.showToast.prototype.test2=function(){console.log("test2")};
abc.edf.commands.showToast.test();
var foo=new abc.edf.commands.showToast;
foo.test2();

为了测试,我将已编译的代码保存到文件" showToast.js"并制作了一个简单的html页面加载它:

console.log("test");
console.log("test2");