使用建構式
function User(name, age, phone) {
this.name = name;
this.age = age;
this.phone = phone;
}
var me = new User('Shawn', 18, '0900123456');
var coworker = new User('Koji', 20, '0911321321');
新增屬性(property)到物件中
me.nationality = 'Taiwan';
新增方法(method)到物件中
coworker.birthYear = function () {
var year = new Date().getFullYear();
return year - this.age;
}
新增屬性(property)到建構式中
新增方法(method)到建構式中(只能在一開始的時候給予,不可用新增屬性的方式)
function UserType2(name, age, phone) {
this.name = name;
this.age = age;
this.phone = phone;
this.nationality = 'Taiwan';
this.changeName = function (name) {
this.name = name;
}
}
沒有留言:
張貼留言