JS快速掌握ES6的class用法
先復(fù)習(xí)一下es5常用的構(gòu)建類的方法:首先es5的寫法使用原型進(jìn)行對象的方法的,為什么不在構(gòu)造函數(shù)里添加方法呢?因?yàn)閷?shí)例化對象的時(shí)候,會重復(fù)的建立好多相同的方法,浪費(fèi)資源。所以需要把對象的方法掛載到prtotype里。
關(guān)于new和this的綁定問題,可以大概簡化為:
首先通過new生成一個(gè)新的對象 然后讓這個(gè)對象綁定到構(gòu)造函數(shù)的this中去 然后綁定這個(gè)構(gòu)造對象的原型對象上 最后把這個(gè)對象返回給前面定義的對象那么接下來看例子吧:
fuction Animal(name,age){ this.name = name this.age = age // 這樣是浪費(fèi)資源的 // this.eat = function(){ // console.log('今天我吃飯了') // }}// 正確做法Animal.prototype.eat=function(){ console.log('今天我吃飯了')}
然后上ES6的class,class很明顯就簡化了這個(gè)操作
cosnt dog = new Animal('wangcai',2) // 會報(bào)錯(cuò),ES6為了修改陋習(xí),和let和const一樣,class不會提升.class Animal{ constroctor(name,age){ this.name = name this.age = age } eat(){ console.log('今天我吃飯了') }}cosnt dog = new Animal('wangcai',2) //正確位置
另外class還添加了靜態(tài)方法,set,get等操作。
class Animal{ constroctor(name,age){ this.name = name this.age = age } eat(){ console.log('今天我吃飯了') } set name(value){ this.tempname ='老鐵'+value } get name(){ return this.tempname } static introuduce(){ console.log('我現(xiàn)在是一個(gè)動物類') }}//set() get()const dog = new Animal('giao',2)dog.name='agiao' console.log(dog.name) // 老鐵agiao// 靜態(tài)方法Animal.introuduce() // 我現(xiàn)在是一個(gè)動物類
再說繼承之前補(bǔ)充個(gè)小知識點(diǎn),class的方法名可以通過計(jì)算屬性的操作來命名
let tempname = 'giao'class Animal{ constroctor(name,age){ this.name = name this.age = age } [tempname](){ console.log('一給我咧giao') }}const xiaoagiao = new Animal('giaoge',30)xiaoagiao.giao() // 一給我咧giao2.繼承
回到繼承這個(gè)問題,es5是怎么繼承的呢?
function Animal( name ){ this.name = name}Animal.prototype.break(){ console.log('叫!')}function Dog( name, age ){ Animal.call(this,name) this.age = age}Dog.prototype = new Animal()Dog.prototype.constructor = Dog
那么這個(gè)叫組合繼承,怎么個(gè)組合法呢?
屬性方面的繼承是借用繼承,可以看到Animal.call(this,name)就是相當(dāng)于把Animal這個(gè)函數(shù)在Dog的構(gòu)造函數(shù)里調(diào)用了一遍而已。雖然屬性他們沒有原型鏈的鏈?zhǔn)铰?lián)通,但是代碼拿過來給Dog都跑了一遍,所以自然就繼承了Animal的name屬性。
Animal.call(this,name)
方法的繼承是原型式繼承,眾所周知,一個(gè)函數(shù)會在創(chuàng)建的時(shí)候生成一個(gè)原型對象,這個(gè)函數(shù)的的一個(gè)protoype屬性指向他的原型對象,原型對象的constructor屬性指向這個(gè)函數(shù)。如果用new來新建這個(gè)函數(shù)的實(shí)例,這個(gè)實(shí)例會有一個(gè)__proto__的屬性指向函數(shù)的原型對象。所以借用函數(shù)實(shí)例會指向函數(shù)原型對象這個(gè)特性,我們將被繼承的函數(shù)實(shí)例化,然后將這個(gè)實(shí)例化的對象賦給繼承的構(gòu)造函數(shù)的prototype屬性,這樣就構(gòu)成了一種鏈?zhǔn)浇Y(jié)構(gòu)。但同被繼承的函數(shù)實(shí)例化是不具備constructor這個(gè)屬性的,我們需要將他的constructor指向繼承的構(gòu)造函數(shù)。
Dog.prototype = new Animal()Dog.prototype.constructor = Dog
所以按照這個(gè)套路,我們用es5的語法,將dog函數(shù)繼承了Animal函數(shù)的name和break方法.
那么ES6是怎么做的呢?
class Animal{ constructor( name ){ this.name = name } break(){ console.log('叫!') }}class Dog extends Animal { constructor( name, age ){ super(name) this.age=age }}
現(xiàn)在只需要在聲明Dog類的時(shí)候加一個(gè)extends Animal,然后再在constructor構(gòu)造函數(shù)里加一個(gè)super就好了。
這個(gè)super(name)就相當(dāng)于Animal.call(this,name)了。然后關(guān)于方法的問題,自然就不用擔(dān)心了,extends自動就處理好了,就不用再去用prototype亂指了.
以上就是JS快速掌握ES6的class用法的詳細(xì)內(nèi)容,更多關(guān)于JS ES6的class用法的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python實(shí)現(xiàn)感知機(jī)模型的示例2. 基于Django集成CAS實(shí)現(xiàn)流程詳解3. springboot多模塊包掃描問題的解決方法4. Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動編譯生成5. 自定義Django默認(rèn)的sitemap站點(diǎn)地圖樣式6. 基于vue實(shí)現(xiàn)探探滑動組件功能7. 深入淺出 妙用Javascript中apply、call、bind8. windows下安裝PHP性能分析工具 xhprof 筆記9. iOS UIScrollView和控制器返回手勢沖突解決方法10. django中url映射規(guī)則和服務(wù)端響應(yīng)順序的實(shí)現(xiàn)
