JavaScript - 建立對象的幾種方式

來源:互聯網
上載者:User

標籤:

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 = function(){        alert(this.name);    };}

    這種方式的缺點在於每個sayName方法都要在每個執行個體上重新建立一遍

    每個方法都是一個Function執行個體,等同於

this.sayName = new Function("alert(this.name)");

    方法對於每個Person執行個體來說,應該是相同的,因此可以通過下面這種不好的方式(可能會增加很多全域函數,並且這些全域函數僅被某個對象調用)解決:

function Person(name, age, job){    this.name = name;    this.age = age;    this.job = job;    this.sayName = sayName;}function sayName(){    alert(this.name);}
3. 原型模式

    每個函數都有一個指向原型對象的屬性,原型對象中的屬性和方法是被共用的,那麼,就可以不把屬性和函數在構造方法中定義,而在原型對象中定義。

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

    如果嫌每次都要敲prototype太繁瑣,大可使用下面這種方式替代:

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

    這種方式的一個弊端就是原型對象是新建立的對象,重寫了原有的原型對象,因此其constructor屬性不再指向Person建構函式,而是指向Object建構函式,這時只能通過instanceof判斷其類型,不能通過constructor判斷其類型了。但是,如果constructor屬性很重要的話,也可通過下面方式設定:

function Person(){}Person.prototype = {    constructor : Person,    name : "Nicholas",    ge : 29,    job: "Software Engineer",    sayName : function () {        alert(this.name);    }};

    這樣又出現了新問題:其constructor屬性變為了可枚舉的,如果JavaScript引擎支援ES5,可使用Object.defineProperty()重設建構函式。

    如果將屬性和方法全在原型中定義,那就有可能會出大事。由於原型對象是被共用的,對於包含參考型別值的屬性來說,一個執行個體的改變會影響所有的執行個體。

function Person(){};Person.prototype = {    friends: ["AAA","BBB"]};var p1 = new Person();var p2 = new Person();p1.friends.push("CCC");alert(p2.friends);// AAA,BBB,CCC
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);    }}

    目前這種方式最常用。

5. 動態原型模式

    定義對象分為建構函式和原型兩部分,看起來可能不太OO,那麼可以在建構函式中初始化原型,讓他表象更OO些。

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

    這裡就不能偷懶不寫prototype了,XX.prototype = {blabla}的方式每次都會重寫原型對象。

6. 穩妥建構函式

    某高人發明了穩妥對象這個概念,就是沒有公用屬性,也不能使用this拿到任何東西。在安全性要求比較高的時候使用,這些環境禁止使用this和new。

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

    這時就只能通過sayName()方法來訪問name屬性了。

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.