javascript 物件導向整理

來源:互聯網
上載者:User

 整理一下js物件導向中的封裝和繼承。

1.封裝

  js中封裝有很多種實現方式,這裡列出常用的幾種。

1.1 原始模式產生對象

  直接將我們的成員寫入對象中,用函數返回。 缺點:很難看出是一個模式出來的執行個體。

代碼:

function Stu(name, score) {
            return {
                name: name,
                score: score
            }
        }

        var stu1 = Stu("張三", 80);
        var stu2 = Stu("李四", 90);

        console.log(stu1.name); // 張三

1.2 產生構造模式對象

  js幫我們提供了一個使用建構函式產生對象的模式,¨所謂“建構函式”,其實就是一個普通函數,但是內部使用了this變數。當使用new關鍵字對建構函式產生執行個體後,this變數則會綁定在執行個體對象上。

  直接上代碼:

function Stu(name, score) {

            this.name = name,
            this.score = score

        }


        var stu1 = new Stu("張三", 80);
        var stu2 = new Stu("李四", 90);


        console.log(stu1.name + "/" + stu2.score); // 張三  90

        console.log((stu1.constructor == Stu) + "/" + (stu2.constructor == Stu)); // true  true

        console.log((stu1 instanceof Stu) + "/" + (stu2 instanceof Stu)); // true  true

 主要方法一般寫在success中,從後台擷取的資料一般是這樣傳過來的: 

不難看出js的建構函式產生對象和C#用class產生對象如出一轍,都是用模板定義對象成員通過new關鍵字執行個體化。

用C#代碼產生同樣的Stu對象

Class Stu
{
public string name;
public double score;                   
}

 

ok,到這兒基本的對象有了。 那麼現在我們需要一個所有對象都公用的方法,而且只讓這個方法建立一次。(不隨著對象new而重複建立)  怎麼辦呢? 大家都知道在C#中我們可以用靜態成員。那麼在js中怎麼做呢?
1.3 Prototype模式

在js中,每一個建構函式都有一個prototype屬性,這個對象的所有屬性和方法,都會被建構函式的執行個體繼承。那麼我們直接給prototype新增成員就相當於在C#中聲明靜態成員了。

代碼:

function Stu(name, score) {
            this.name = name,
            this.score = score
        }

        Stu.prototype.type='學生';
        Stu.prototype.log = function (s) {
            console.log(s);
        }

        var stu1 = new Stu("張三", 80);
        var stu2 = new Stu("李四", 90);

        console.log(stu1.type + "/" + stu2.type); // 學生 學生
        stu1.log('hello');  // hello
        console.log(stu1.log == stu2.log);  // true

封裝就講到這兒了,下面我們來看看js中繼承又是如何?的?

2.繼承

2.1 建構函式綁定  在子函數中直接調用 call或apply方法,將父物件的建構函式綁定在子物件上。function Stu(name, score) {
            Grade.apply(this, arguments);
            //Grade.call(this, arguments);
            this.name = name,
            this.score = score
        }

        function Grade() {
            this.code = "初中";
            this.ask = function () {
                console.log("大家好");
            }
        }

        var stu1 = new Stu("張三", 80);
        var stu2 = new Stu("李四", 90);

        console.log(stu1.code); // 初中
        stu1.ask(); // 大家好 這裡的apply做了兩件事情,把第一個參數this給Grade建構函式(調用者),然後再執行Grade裡的代碼。就相當於將Grade中用this定義的成員在Stu中再執行一遍。2.2 通過prototype繼承

  先看代碼

代碼:

function Stu(name, score) {

            this.name = name,
            this.score = score
        }

        function Grade() {
            this.code = "初中";
        }

        Stu.prototype = new Grade();

        Stu.prototype.constructor = Stu; //防止繼承鏈的紊亂,手動重設聲明

        var stu1 = new Stu("張三", 80);
        var stu2 = new Stu("李四", 90);

        console.log(Stu.prototype.constructor); // 自己的建構函式
        console.log(stu1.code); // 初中

前面說過prototype就相當於C#中的靜態成員,所以我們就把父類的所有成員都變成自己的靜態成員來實現繼承。

  通過prototype繼承有一個缺點:所有繼承的成員都是靜態,那麼怎麼繼承對象成員呢?

2.3 拷貝繼承

  把父物件的所有屬性和方法,拷貝進子物件,實現繼承。

代碼:

function Stu(name, score) {
            this.name = name,
            this.score = score
        }

        function Grade() {}
        Grade.prototype.code = "初中";
    }
        //函數封裝
        function extend(C, P) {
            var p = P.prototype;
            var c = C.prototype;
            for (var i in p) {
                c[i] = p[i];
            }
        }
        extend(Stu, Grade);
        var stu1 = new Stu("張三", 80);
        var stu2 = new Stu("李四", 90);
        stu1.code='高中';
        console.log(stu1.code); // 高中
        console.log(stu2.code); // 初中
        console.log(Stu.prototype.constructor);
        console.log(Grade.prototype.constructor)

    js物件導向的整理就寫到這了,這個東西也不是一成不變的,使用的時候根據自己的需求做改動。  有句話說的很好,合適的才是最好的。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.