在codecademy.com上浏览javascript课程时,我有点困惑.
首先,我们一直在学习如何向类添加方法:
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();
Run Code Online (Sandbox Code Playgroud)
然后我们开始了"原型".还有这个例子:
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();
Run Code Online (Sandbox Code Playgroud)
两个例子都给出了相同的结果.这两个例子只是两种做同样事情的方式吗?或者两者之间存在实际差异?