JavaScript创建对象

创建对象

1.工厂模式

function createPerson(name, age, job){     
   var o = new Object();     
   o.name = name;     
   o.age = age;     
   o.job = job;     
   o.sayName = function(){         
       alert(this.name);     
   };         
   return o;
} 

var person1 = createPerson("Nicholas", 29, "Software Engineer"); 
var person2 = createPerson("Greg", 27, "Doctor"); 

2.构造函数模式

function Person(name, age, job){     
    this.name = name;     
    this.age = age;     
    this.job = job;     
    this.sayName = sayName; 
} 
 
function sayName(){     
    alert(this.name); 
} 
 
var person1 = new Person("Nicholas", 29, "Software Engineer"); 
var person2 = new Person("Greg", 27, "Doctor");

3.原型模式

function Person(){ } 
Person.prototype.name = "Nicholas"; Person.prototype.age = 29; 
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function(){alert(this.name);}; 
 
var person1 = new Person(); 
person1.sayName();   //"Nicholas" 
 
var person2 = new Person(); 
person2.sayName();   //"Nicholas"

alert(person1.sayName == person2.sayName);  //true

4.组合使用构造函数模式和原型模式

function Person(name, age, job){     
    this.name = name;     
    this.age = age;     
    this.job = job;     
    this.friends = ["Shelby", "Court"]; 
} 
 
Person.prototype = {     
    constructor : Person,     
    sayName : function(){                            alert(this.name);     
    }
} 
 
var person1 = new Person("Nicholas", 29, "Software Engineer"); 
var person2 = new Person("Greg", 27, "Doctor"); 
 
person1.friends.push("Van");
alert(person1.friends);    //"Shelby,Count,Van"
alert(person2.friends);    //"Shelby,Count"
alert(person1.friends === person2.friends); 
//false alert(person1.sayName === person2.sayName);    //true 

5.动态原型模式

function Person(name, age, job){ 
    //属性     
    this.name = name;     
    this.age = age;     
    this.job = job; 
     //方法     
     if (typeof this.sayName != "function"){        
     Person.prototype.sayName = function(){
            alert(this.name);    
        };             
     } 
} 
 
var friend = new Person("Nicholas", 29, "Software Engineer"); 
friend.sayName();

6.寄生构造函数模式

function Person(name, age, job){     
    var o = new Object();     
    o.name = name;     
    o.age = age;     
    o.job = job;     
    o.sayName = function(){ 
        alert(this.name);     
    };        
    return o; 
} 
 
var friend = new Person("Nicholas", 29, "Software Engineer"); 
friend.sayName();  //"Nicholas

7.稳妥构造函数模式

function Person(name, age, job){
    //创建要返回的对象     
    var o = new Object();
    //可以在这里定义私有变量和函数
    
    //添加方法     
    o.sayName = function(){
        alert(name);    
    };              
    //返回对象    
    return o;
} 

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