1
function Fruit (type) {
this.type = type;
this.color = 'unknown';
}
Fruit.prototype.getInformation = function() {
return 'This ' + this.type + ' is ' + this.color + '.';
}
let lime = new Fruit('Mexican lime');
console.log(lime.getInformation());
lime.color = 'green';
console.log(lime.getInformation());
2
function Fruit (type) {
this.type = type;
this.color = 'unknown';
this.getInformation = function() {
return 'This ' + this.type + ' is ' + this.color + '.';
}
}
let lime = new Fruit('Mexican lime');
console.log(lime.getInformation());
lime.color = 'green';
console.log(lime.getInformation());
我无法区分这两个代码;就像原型的功能到底有什么用呢?