我刚刚开始在 JavaScript 中使用模块模式来声明函数对象,但发现在声明您可能需要的所有公共(public)函数时重复 Foo.prototype 关键字很烦人。
因此,为了方便起见,我想出了使用一些简写变量,如下所示。
(function(){
var Foo = function(){
alert('created Foo');
}
// shorthand for further creation of public functions
var pro = Foo.prototype;
//public function with shorthand
pro.bar = function(){
//some cool stuff here
}
return window.Foo = Foo;
}());
问题是:是否有某种原因阻止我保存这几个有关功能或意外错误的字符,或者这样做是否安全? 这对于我使用它的情况来说似乎工作得很好?
最佳答案
据我所知,原型(prototype)在修改方面与其他对象一样。保留对它的引用绝对没问题
关于javascript - 使用变量作为 Foo.prototype 的简写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21367552/