雖然在codecademy.com上通過javascript課程,我已經變得有點困惑。有人可以告訴我區別嗎? javascript oop
所以首先我們已經learnring如何添加方法的類:
function Dog (breed) {
this.breed = breed;
this.sayHello = function() {
console.log("Hello this is a " + this.breed + " dog");
}
};
var someDog = new Dog("golden retriever");
someDog.sayHello();
然後,我們開始了「原型」。有這樣的例子:
function Dog (breed) {
this.breed = breed;
};
Dog.prototype.sayHello = function() {
console.log("Hello this is a " + this.breed + " dog");
}
var someDog = new Dog("golden retriever");
someDog.sayHello();
這兩個例子都給出了相同的結果。 這兩個例子只是兩種做同樣的事情?或者兩者之間有實際區別?
接得好!!!!! +1 –