javascript生成对象的三种方法

时间:2023-03-08 16:58:12
/** js生成对象的三种方法*/

// 1.通过new Object,然后添加属性  示例如下:

var people1 = new Object();
people1.name = 'xiaohailong01';
people1.age = 31; alert("name:" + people1.name + ", age:" + people1.age); // 2.直接接收json数据生成 var people2 = {
name : 'xiaohailong02',
age : 32
}; alert("name:" + people2.name + ", age:" + people2.age); // 3.通过function方法(模拟类) function People (name, age) {
this.name = name;
this.age = age;
} var people3 = new People('xiaohailong03', 33);
alert('name:' + people3.name + ', age:' + people3.age);