JavaScript類繼承實現之一,javascript類繼承

來源:互聯網
上載者:User

JavaScript類繼承實現之一,javascript類繼承

網上各部落格論壇出現了很多JavaScript的類繼承方法,瀏覽了一些,不是沒注釋,就是沒有運行效果,於是自己寫了個簡潔易懂的版本,附帶測試代碼在最下面。

剛不小心刪除了該文章,因此重新補上,另外加強了樣本。

(function() {

    Function.prototype.extend = function(baseClass) {

        // this is a function object.
        var oldPrototype = this.prototype, newPrototype = {}, _super = new baseClass();

        //inherits all properties and methods.
        for ( var name in _super) {
            newPrototype[name] = _super[name];
        }

        for ( var name in oldPrototype) {
            // only override properties in this new Class.
            if (oldPrototype.hasOwnProperty(name)) {
                // only function need _super.
                if (typeof oldPrototype[name] == "function" && typeof _super[name] == "function") {
                    newPrototype[name] =  (function(name, fn) {
                            return function() {
                                var tmp = this._super;
                                
                                // set super method
                                this._super = _super[name];

                                var ret = fn.apply(this, arguments);
            
                                this._super = tmp;
            
                                return ret;
                            };
                        })(name, oldPrototype[name]);
                }
            }
        }

        this.prototype = newPrototype;
        
        return this;
    };
})();

var A = function() {
    this.hello = function() {
        console.log("hello, I'm A");
    }
};

var B = function() {};

B.prototype = {
    hello : function() {
        this._super();

        console.log("hello, I'm B");
    }
};

B.extend(A);

var C = function() {};

C.prototype = {
    hello : function() {
        this._super();

        console.log("hello, I'm C");
    }
};

C.extend(B);

var b = new B();
var c = new C();

//b.hello();
c.hello();
在JavaScript裡怎實現Java式類繼承?

js繼承有5種實現方式: 1、繼承第一種方式:對象冒充 function Parent(username){ this.username = username; this.hello = function(){ alert(this.username); } } function Child(username,password){ //通過以下3行實現將Parent的屬性和方法追加到Child中,從而實現繼承 //第一步:this.method是作為一個臨時的屬性,並且指向Parent所指向的對象, //第二步:執行this.method方法,即執行Parent所指向的對象函數 //第三步:銷毀this.method屬性,即此時Child就已經擁有了Parent的所有屬性和方法 this.method = Parent; this.method(username);//最關鍵的一行 delete this.method; this.password = password; this.world = function(){ alert(this.password); } } var parent = new Parent("zhangsan"); var child = new Child("lisi","123456"); parent.hello(); child.hello(); child.world(); 2、繼承第二種方式:call()方法方式 call方法是Function類中的方法 call方法的第一個參數的值賦值給類(即方法)中出現的this call方法的第二個參數開始依次賦值給類(即方法)所接受的參數 function test(str){ alert(this.name + " " + str); } var object = new Object(); object.name = "zhangsan"; test.call(object,"langsin");//此時,第一個參數值object傳遞給了test類(即方法)中出現的this,而第二個參數"langsin"則賦值給了test類(即方法)的str function Parent(username){ this.username = username; this.hello = function(){ alert(this.username); } } function Child(username,password){ Parent.call(this,username); this.password = password; this.world = function(){ alert(this.password); } } var parent = new Parent("zhangsan"); va......餘下全文>>
 
javascript實現繼承

應該類似
Array.prototype.mymethod=function(){
alert('mymethod()');
}
var b = new Array();
b.mymethod();
將一些類中的方法重寫了,或者另外定義了自己的方法。比如上面,在當前頁面中就可以使用Array中不存在的mymethod();方法了。
啊咯,那個,我也比較懶,不想去研究jquery的具體代碼.....
 

聯繫我們

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