JavaScript(十三)物件導向

來源:互聯網
上載者:User

標籤:品質   計時   ast   這一   his   []   效能   src   cal   

  物件導向

物件導向的過程

  通過new 建構函式 產生的對象來執行, 類似於事件的執行  this指向函數,然後再把這個函數賦值給一個執行個體  所以在函數內的this  就指到了執行個體上

function Person (age,lastname,) {      this.age = age      this.lastname = lastname;    } var xiaoming = new Person(12,‘小明‘); console.log(xiaoming);

 

建構函式的首字母要大寫   大家預設的規範

對象中若  value 非函數  叫做屬性   若 是函數  叫方法

this的指向

  普通對象,計時器,定時器,this 品質window 

  對象中的方法的this  指向這個對象,這個對象就和windows類似

  綁定事件時 this指向 dom對象

  閉包的this 指向上次啟動並執行函數環境

  自執行函數  指向window

  對象中的定時器等的this仍是指向window 所以需要在對象內備份 this 

執行個體與建構函式的關係

  新產生的對象  有個屬性 constructor      就是這個對象的建構函式

  執行個體  instanceof 建構函式  ---->true

console.log(xiaoming.constructor);console.log(xiaoming instanceof Person);

 

物件導向中的類的概念

  就是 建構函式  ,同一個建構函式  new出來的 就屬於一個類

原型

  建構函式方法存在一定的弊端,對於對象中一成不變的屬性 並不需要每次都new ,這樣很浪費效能,那麼  就把這用一成不變的儲存到   建構函式的原型上面

    function Person (age,name,) {      this.age = age;      this.name = name;    }    Person.prototype.sex = ‘male‘;    var xiaoming = new Person(12,‘xiaoming‘);    console.log(xiaoming.sex);//這個性別就是從原型上繼承的

  建構函式的原型除了以    fun.prototype.attr = ***; 的格式 

 

  還可以   寫整個 原型    

Person.prototype = {    age : 12,    sex:‘male‘,    constructor : Person //這裡如果不寫 new的執行個體調用constrotur 不會返回person 而返回 Object}

 

 

prototype和__proto__

 prototype       所有的函數都有原型prototype   

 .__proto__    所有的對象都有原型對象   ∵所有的對象都是用建構函式建立的

constructor,protype與__proto__

 

判斷這個執行個體是不是這個建構函式new的

  建構函式的原型 .isProtypeOf(新對象)

  

利用 in 結合 hasOwnProperty判斷是不是從原型繼承來的屬性

for (var pro in xiaoming) {  if(xiaoming.hasOwnProperty(pro)){        console.log(pro + ‘是區域屬性‘)    }  }

 Binder 建構函式    兩個對象(建構函式之間)繼承

  1.call / apply 用父類     代替子類的this  

    父建構函式.call(this,[arguments])  

 function Foo () {     this.lastName = ‘wang‘;   }  function Son (height) {    Foo.call(this);  //就這一步就可以繼承來 Foo的屬性    this.height = height;  }  var xw = new Son(180);  console.log(xw.lastName);  //wang

 

 

prototype的繼承方法

  寫在perotype 上的屬性在產生執行個體的時候不會被 調用   所以 執行個體上的屬性由繼承來的更好   提取公用部分在原型上

    function Foo() {      this.firestName = ‘老王‘;    }    Foo.prototype.lastName = ‘wang‘;    function Son() {      this.firstName = ‘小王‘;    }    Son.prototype = new Foo();    Son.prototype.constrotur = Son;    var xw = new Son();    console.log(xw.firstName);    console.log(xw.lastName);    console.log(xw.constrotur);

 利用空的函數的prototype 實現繼承

function extend(parent, child) {var F = function(){};F.prototype = parent.prototype;child.prototype = new F();child.prototype.constructor = child;child.uber = parent.prototype;}

拷貝繼承 就是把parent.protype 上的屬性 遍曆拷貝到 child 的protype上

 

 

function copy(parent, child) {      var p = parent.prototype;      var c = child.prototype;      for (var pro in p) {        c[pro] = p[pro]; //這裡的pro是屬性名稱!!!!是字串形式的  所以只能用[]的形式      }      c.uber = p;    }

 

JavaScript(十三)物件導向

相關文章

聯繫我們

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