js创建object对象主要有以下四种方法:
使用new操作符后跟Object构造函数
var person = new Object();//可以写成 var person = {}; person.name = "kate"; person.age = 18;
使用“对象字面量”表示法
var person = { name : "kate", age:18 };
工厂模式方法
var createPerson = function(name,age,job){ var person = new Object(); person.name = name; person.age = age; person.job = job; person.sayName = function(){ alert(person.name); } return person; } var person1 = createPerson("zh","62","Doctor"); person1.sayName();
构造函数模式方法
function Person(name,age,job){ this.name = name; this.age = age; this.job = job; this.sayName = function(){ alert(this.name); }; }; var person1 = new Person("zhou",23,"test"); person1.sayName();
声明:如需转载,请注明来源于www.webym.net并保留原文链接:http://www.webym.net/jiaocheng/248.html