javascript生成對象的三種方法

/** 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);
相關文章
相關標籤/搜索