好的,我正在学习一些主题,而且我正在学习原型。我看到这有效:
function Person(name) {
this.name = name || 'Luis Felipe Zaguini';
this.sayHi = function() {
console.log(`Hey there, ${this.name}!`);
}
this.sayGoodBye = function() {
console.log(`Good bye, ${this.name}!`);
}
}
let me = new Person();
me.sayHi();
let other = new Person('Other');
other.sayGoodBye();
但这也有效:
function Person(name) {
this.name = name || 'Luis Felipe Zaguini';
this.sayHi = function() {
console.log(`Hey there, ${this.name}!`);
}
}
Person.prototype.sayGoodBye = function() {
console.log(`Good bye, ${this.name}!`);
}
let me = new Person();
me.sayHi();
let other = new Person('Other');
other.sayGoodBye();
他们以同样的方式结束了#39;。哪个版本更好?有一些缺点吗?在我看来,第一种方式(在函数对象内定义方法)看起来更清晰。你们的人怎么想?