私は Javascript プロトタイプをいじっていましたが、なぜこれが機能するのかわかりません:
function User(un) {
this.username = un;
}
User.prototype.method_name = function() {
return this.username;
};
var user = new User("Michael");
console.log(user.method_name());
しかし、これはしません:
function User(un) {
this.username = un;
return{
getUsername: function (){
return username;
},
setUsername: function(username) {
username = un;
}
};
}
User.prototype.method_name = function() {
return this.username;
};
var user = new User("Michael");
console.log(user.method_name());
「return」ステートメントを追加するとスローされるのはなぜObject #<Object> has no method 'method_name'
ですか?