js call()方法

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   sp   on   div   log   

 1 //用add方法替換sub方法 2 function add(a,b){ 3   console.log(a+b) 4 } 5 function sub(a,b){ 6   console.log(a-b) 7 } 8 add.call(sub,3,1); 9 10 //c1的方法放到c2上執行11 function Class1(){12   this.cName = ‘class1‘;13   this.showClass = function(){14     console.log(this.cName)15   }16 }17 function Class2(){18   this.cName = ‘class2‘;19 }20 var c1 = new Class1();21 var c2 = new Class2();22 c1.showClass.call(c2);23 24 //實現繼承 用Class3對象代替Class4的this對象,Class4即擁有所有Class3的屬性和方法25 function Class3(){26   this.showTxt = function(txt){27     console.log(txt)28   }29 }30 function Class4(){31   Class3.call(this)32 }33 var c4 = new Class4();34 c4.showTxt(‘hello‘);35 36 //多重繼承37 function Add() {38   this.showAdd = function(a, b) {39     console.log(a+b)40   }41 }42 function Sub() {43   this.showSub = function(a, b) {44     console.log(a-b)45   }46 }47 function Count(){48   Add.call(this);49   Sub.call(this);50 }51 var c = new Count();52 c.showAdd(1,3);53 c.showSub(1,3);

 

js call()方法

聯繫我們

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