JS中创建对象有两种方法:一个复杂的,一个是紧凑的。
复杂的:
function people (name, age){
this.age=age;
this.name=name;
}
var xiaoMing=new people("xiaoMing", 12);
紧凑型:
var xiaoMing={name:"xiaoMing", age:"12"};
alert(xiaoMing.name); //调用对象的属性
使用 prototype (原型)属性来向对象添加属性:
people.prototype.address=function(){
alert("address is 北京市丰台区");
}
people.prototype.address="北京市朝阳区";
var liLei=new people(liLei, 50);
var liLei.address();//调用方法,别忘记写();
var x=liLei.address; //调用新增的属性