js中的OOP編程

來源:互聯網
上載者:User

標籤:操作   常見   rip   ber   建立   方法   封裝   一段   add   

本文適用於es6之前。。。

javascript需要用函數來類比類。

function Book(name) {    this.name = name;    this.getName = function () {            return this.name;      }      this.setName = function (nname) {             this.name = nname;      } }

new 一個對象

var book1 = new Book("C#");//這裡的new操作相當於先建立了一個簡單對象,調用了類的建構函式

每一個function上面都有一個原型對象 --prototype

var proto = Book.prototype;  proto.str = "string";  proto.hello = function () {       alert("Hello");  }  //給原型定義了屬性和方法後,擁有這個原型對象的function類比出來的類,也具有該屬性和方法       alert(book1.str); //彈出string       book1.hello(); //彈出hello  }  

補充一段常見的問題

function animal(name) {      this.name = name;      this.age = 0;  }      var a1 = animal;      alert(a1); // 彈出整個函數體      var a2 = animal("dinglang");      alert(a2); //彈出undefined      var a3 = new animal();      alert(a3);//彈出object      var a4 = new animal;      alert(a4);//彈出object  

類的修改,擴充(重點,痛點) 

//1.假如我要實現一個簡單的加法計算              var numOne = 5; //如果直接這麼定義,那麼下面的numOne.add(8);執行會報錯              //如果我這麼寫,下面調用就不會報錯了(因為此時的numOne,是個類.相當於java或C#語言中原始的基礎資料型別 (Elementary Data Type)、封裝類型)              var numOne = new Number(5);              numOne.add = function (numTwo) {                  return this + numTwo;              }              alert(numOne.add); //undefined              alert(numOne.add(8));//這樣寫看起來沒錯,但是會報錯--numOne.add is not a function              var numThree = new Number(100);              //如果我現在想要給numThree對象中也加上add這麼一個函數              //直接使用prototype這個特殊的屬性來實現,給所有的Number類型執行個體都加入add函數              Number.prototype.add = function (numTwo) {                  return this + numTwo;              }                alert(numThree.add(200).add(300)); //彈出600   100+200+300=600   

實現javascript中的繼承

 function classA(name) {                  this.name = name;                  this.showName = function () {                      alert(this.name);                  }              }              function classB(name) {                  //1)使用newMethod的方式實現繼承                  //                this.newMethod = classA;                  //                this.newMethod(name);                  //                delete this.newMethod; //釋放對象                  //2)調用claasA這個函數,並把他的上下文(範圍)指向this(也就是classB類的執行個體)                  //這樣也能實現繼承效果(使用call或者apply)                  classA.call(this, name);                  //classA.apply(this,[name]);              }              objA = new classA("作業系統");              objB = new classB("組成原理");              objA.showName(); //彈出“作業系統”              objB.showName(); //彈出“組成原理”            })

 

js中的OOP編程

聯繫我們

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