var Member = function(firstname,lsatname){
this.firstname = firstname;
this.lastname = lastname;
this.getname = function(){
this.firstname+this.lastname;}
};
var num1 = new Member("kouichi","usui");
console.log(num.getname);
var num2 = new Member("Nami","kakeya");
console.log(num.getname);
var num3 = new Member("Tosi","jojo");
console.log(num.getname);
by ChatGPT
var Member = function(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
};
Member.prototype.getname = function() {
return this.firstname + this.lastname;
};
var num1 = new Member("kouichi", "usui");
var num2 = new Member("Nami", "kakeya");
var num3 = new Member("Tosi", "jojo");
建議使用新的寫法,如下
class Member {
constructor(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
getname() {
return this.firstname + this.lastname;
}
}
const num1 = new Member("kouichi", "usui");
const num2 = new Member("Nami", "kakeya");
const num3 = new Member("Tosi", "jojo");
這兩個方法都不會造成重複複製getname