javascript 建立對象的幾種方式

來源:互聯網
上載者:User

標籤:

1、通過new Object建立

建立自訂對象的最簡單方式就是建立一個Object 的執行個體,然後再為它添加屬性和方法,如下所示。

var person = new Object();person.name = "Nicholas";person.age = 29;person.job = "Software Engineer";person.sayName = function(){alert(this.name);};

2、通過對象字面量建立對象

var person = {  name: "Nicholas",  age: 29,  job: "Software Engineer",  sayName: function(){  alert(this.name); }};

3、通過建構函式模式建立

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

4、通過原型模式建立

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

5、組合使用建構函式模式和原型模式

  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

6、動態原型模式

 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();

7、寄生建構函式模式  

    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"

8、穩妥建構函式模式

    function Person(name, age, job){        //建立要返回的對象        var o = new Object();        //可以在這裡定義私人變數和函數        //添加方法        o.sayName = function(){            alert(name);        };        //返回對象        return o;    }

  

  

 

  

  

 

  

javascript 建立對象的幾種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.