function student(props){ this.name=props.name || ‘匿名‘;//默认是匿名 this.grade=props.grade || 1; } student.prototype.hello=function(){ console.log(‘hello ‘+this.name); } function createStudent(props){ return new student(props||{}) } var xiaoming=createStudent({ name:‘xiaoming‘ }); xiaoming.hello();//hello xiaoming
传进一个数组
function animal(name,age,grade){ this.name=name; this.age=age; this.grade=grade; } animal.prototype.hello=function(){ console.log(‘hello ‘+this.name); } var chicken=new animal(‘chicken‘,3,12); chicken.hello();//hello chicken
我理解的构造函数就是用new()实例化去构造
原文:http://www.cnblogs.com/lwwen/p/6231839.html