我试图了解在 Javascript 中处理 OOP 的方式,我偶然发现了 coffescript 定义构造函数和原型的方式。有人能告诉我这段代码有什么好处吗
Animal = (function() {
function Animal(name) {
this.name = name;
}
Animal.prototype.move = function(meters) {
return alert(this.name + (" moved " + meters + "m."));
};
return Animal;
})();
与此相比
function Animal(name) {
this.name = name;
}
Animal.prototype.move = function(meters) {
return alert(this.name + (" moved " + meters + "m."));
};