亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        javascript 面向對象封裝與繼承

        字號:


            整理一下js面向對象中的封裝和繼承。
            1.封裝
            js中封裝有很多種實現(xiàn)方式,這里列出常用的幾種。
            1.1 原始模式生成對象
            直接將我們的成員寫入對象中,用函數(shù)返回。 缺點:很難看出是一個模式出來的實例。
            代碼:
            代碼如下:
            function Stu(name, score) {
            return {
            name: name,
            score: score
            }
            }
            var stu1 = Stu("張三", 80);
            var stu2 = Stu("李四", 90);
            console.log(stu1.name); // 張三
            1.2 生成構造模式對象
            js幫我們提供了一個使用構造函數(shù)生成對象的模式,¨所謂“構造函數(shù)”,其實就是一個普通函數(shù),但是內部使用了this變量。當使用new關鍵字對構造函數(shù)生成實例后,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
            不難看出js的構造函數(shù)生成對象和C#用class生成對象如出一轍,都是用模板定義對象成員通過new關鍵字實例化。
            用C#代碼生成同樣的Stu對象
            代碼如下:
            Class Stu
            {
            public string name;
            public double score;
            }
            ok,到這兒基本的對象有了。 那么現(xiàn)在我們需要一個所有對象都公用的方法,而且只讓這個方法創(chuàng)建一次。(不隨著對象new而重復創(chuàng)建)怎么辦呢? 大家都知道在C#中我們可以用靜態(tài)成員。那么在js中怎么做呢?
            1.3 Prototype模式
            在js中,每一個構造函數(shù)都有一個prototype屬性,這個對象的所有屬性和方法,都會被構造函數(shù)的實例繼承。那么我們直接給prototype添加成員就相當于在C#中聲明靜態(tài)成員了。
            代碼:
            代碼如下:
            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中繼承又是如何實現(xiàn)的?
            2.繼承
            2.1 構造函數(shù)綁定
            在子函數(shù)中直接調用 call或apply方法,將父對象的構造函數(shù)綁定在子對象上。
            代碼如下:
            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做了兩件事情,把第一個參數(shù)this給Grade構造函數(shù)(調用者),然后再執(zhí)行Grade里的代碼。就相當于將Grade中用this定義的成員在Stu中再執(zhí)行一遍。
            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); // 自己的構造函數(shù)
            console.log(stu1.code); // 初中
            前面說過prototype就相當于C#中的靜態(tài)成員,所以我們就把父類的所有成員都變成自己的靜態(tài)成員來實現(xiàn)繼承。
            通過prototype繼承有一個缺點:所有繼承的成員都是靜態(tài)的,那么怎么繼承對象成員呢?
            2.3 拷貝繼承
            把父對象的所有屬性和方法,拷貝進子對象,實現(xiàn)繼承。
            代碼:
            代碼如下:
            function Stu(name, score) {
            this.name = name,
            this.score = score
            }
            function Grade() {}
            Grade.prototype.code = "初中";
            }
            //函數(shù)封裝
            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面向對象的整理就寫到這了,這個東西也不是一成不變的,使用的時候根據(jù)自己的需求做改動。 有句話說的很好,合適的才是最好的。