自定义对象
实例化对象
var person=new Object();
person.name="Lisa";
person.getName=function (){
return "Name is" + this.name; //this:当前对象person
}
定义对象
var person = {
firstname: "Wang",
lastname: "tian",
get fullname() {
return this.firstname + this.lastname;
},
set name(name) {
name = name;
console.log(name);
}
}
person.name = "lisa";//set
console.log(person);
属性、方法操作方法
添加新的属性
添加新的属性 使用get set 方法 来追加;
setter set 设置属性名称;getter get 返回属性名称
Object.defineProperty(obj,"prop",{get:fn,set:fn})
function person() {
this.name = "lisa";
}
var per = new person();//new函数为对象
//给实例化对象 添加属性
Object.defineProperty(per, "age", {
get: function () {
return age;
},
set: function (a) {
age = a;
}
});
per.age = 18;