创建对象的方法

  
    
// 创建对象
var o1 = new Object();
o1.property1
= ' 1 ' ;
o1.property2
= ' 2 ' ;

// 工厂模式
function createObject(property1,property2){
var o = new Object();
o.property1
= property1;
o.property2
= property2;
return o;
}
var o1 = createObject( ' 1 ' , ' 2 ' );

// 构造函数模式(构造函数首字母大写)
function O(property1,property2){
this .property1 = property1;
this .property2 = property2;
}
var o1 = new O( ' 1 ' , ' 2 ' );

// 原型模式
function O(){
}
O.prototype.property1
= ' 1 ' ;
O.prototype.property2
= ' 2 ' ;

var o1 = new O();

你可能感兴趣的:(对象)