Javascript 是否';命令';在谷歌关闭有原型吗?
我正在使用Google Close,并尝试执行以下操作:Javascript 是否';命令';在谷歌关闭有原型吗?,javascript,google-closure-compiler,google-closure,Javascript,Google Closure Compiler,Google Closure,我正在使用Google Close,并尝试执行以下操作: 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 funct
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中的每个对象都有“原型”。那是因为“命令”不是对象吗?还是我错过了什么?谢谢:)正如Felix Kling评论的那样,您需要使用
new
来创建一个对象,以便使用test2
原型功能。以下是一个例子:
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();
以下是它使用高级编译编译的目的:
console.log("test");
console.log("test2");
为了进行测试,我将编译后的代码保存到一个文件“showtoos.js”中,并制作了一个简单的html页面来加载它:
<!doctype html>
<html>
<head>
</head>
<body>
<p>ShowToast Test</p>
<script src="showToast.js"></script>
</body>
</html>
展示测试
正如Felix Kling评论的那样,您需要使用new
创建一个对象,以便使用test2
原型功能。以下是一个例子:
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();
以下是它使用高级编译编译的目的:
console.log("test");
console.log("test2");
为了进行测试,我将编译后的代码保存到一个文件“showtoos.js”中,并制作了一个简单的html页面来加载它:
<!doctype html>
<html>
<head>
</head>
<body>
<p>ShowToast Test</p>
<script src="showToast.js"></script>
</body>
</html>
展示测试
可能此
未引用abc.edf.commands.showtoos的实例。您需要使用new
调用函数才能使其工作。可能此
未引用abc.edf.commands.showtoos
的实例。您需要使用new
调用函数才能使其工作。