JS对象创建方法汇总
直接方法:字面量or new Object()
其constructor属性指向Object构造函数;其prototype为undefined;原型链上端是Object.prototype,即:
var obj=new Object();
alert(obj.__proto__==Object.prototype);//true,仅chrome支持
alert(obj.constructor==Object);//true
Object.create()*1方法
用一个对象去创建另一个对象,两个对象之间的是继承的关系。
如果用null去创建一个对象,则创建的对象没有toString()等方法。
var obj1 = Object.create({x:1,x:2});
var obj2 = Object.create(null);
Object.create()*方法和下面的函数作用相同,可以在不支持该方法的浏览器中使用下面这个函数
function object(o){
function F(){}
F.prototype=o;
return new F();
}
复杂方法
构造函数模式
构造函数没有return语句,应该以一个大写字母开头,通常定义在Global对象(window对象)中,每个实例都有自己的属性和方法。
每个实例都包含不同Function实例,导致不同的作用域链,和标识符解析。
对于没有自定义方法的对象可以用哦
function Student(name,id,major){
this.name=name;
this.id=id;
this.major=major;
this.showId=function(){
console.log(this.id);
}
}
var xiaoming=new Student('xiaoming','001','Math');
var xiaohong=new Student('xiaohong','002','English');
原型模式
构造函数为空,创建构造函数的原型对象来实现所有实例对象的共享属性和方法。
由于其共享的本质,对于引用类型的属性值,往往实例之间会相互影响。而对象实例没有自己的属性。实际很少用
function Student(){}
Student.prototype.id='001';
Student.prototype.name='xiaoming';
Student.prototype.major='English';
Student.prototype.showId=function(){
console.log(this.id);
};
var xiaoming=new Student();//xiaoming.name='xiaoming'
var xiaohong=new Student();//xiaohong.name='xiaoming'
这时,Student.prototype.constructor=Student
或用对象字面量的方式定义Student.prototype
function Student(){}
Student.prototype={
id:'001',
name:'xiaoming',
major:'English'.
showId:function(){
console.log(this.id);
}
};
var xiaoming=new Student();//xiaoming.name='xiaoming'
var xiaohong=new Student();//xiaohong.name='xiaoming'
这时,Student.prototype.constructor=Object
构造函数+原型模式
组合使用构造函数模式和原型模式。构造函数用于定义实例属性,而原型模式用于定义方法和共享的属性。
使用最广泛、认同度最高的一种方式,默认模式。
function Student(id,name){
this.id=id;
this.name=name;
}
Student.prototype={
major:'English',//共享的属性,即专业都为英语
showId:function(){
console.log(this.id);
}
};
Object.defineProperty(Student.prototype,'constructor',{
enumerable:false.
value:Student
});// 仅适用于支持ECMAScript5以上的浏览器,使得Student.prototype.constructor=Student,仅在constructor有重要用途时候用!
var xiaoming=new Student('001','xiaoming');
var xiaohong=new Student('002','xiaohong');
动态原型模式
把所有信息都封装在了构造函数中,通过构造函数初始化原型,通过if语句检查,然后添加原型。
不能使用对象字面量重写原型!!!
function Student(id,name){
this.id=id;
this.name=name;
if(typeof this.showId != 'function'){
Student.prototype.major='English';
Student.prototype.showId=function(){
console.log(this.id);
}
}
}
var xiaohong=new Student('001','xiaohong');//xiaohong.name='xiaohong'
且这时,Student.prototype.constructor=Student
寄生构造函数模式
就是将创建对象的代码封装起来,返回新建的对象。
可以在特殊情况下为对象创建构造函数,可以使用其他模式的情况不要使用这种模式。
function Student(id,name,major){
var stu = new Object();
stu.id=id;
stu.name=name;
stu.major=major;
stu.showId=function(){
console.log(this.id);
};
return stu;
}
var xiaoming=new Student('001','xiaoming','English');
这种情况下,xiaoming.constructor=Object,xiaoming.__proto__=Object.prototype
稳妥构造函数模式
与寄生构造函数模式类似,适用于安全的环境中,
新创建的对象的实例方法不引用this
,不使用new
调用构造函数。
function Student(id,name,major){
var stu = new Object();
stu.showId=function(){
console.log(id);
};
return stu;
}
var xiaoming=Student('001','xiaoming','English');
这种情况下,xiaoming.id
为undefined
,只有通过xiaoming.showId()
得到id
。
xiaoming.constructor=Object,xiaoming.__proto__=Object.prototype
- *号表示ECMAScript5新增的方法,需要较新版本的浏览器支持。 ↩