ES5和ES6中類(lèi)的區(qū)別總結(jié)
類(lèi)定義與調(diào)用的區(qū)別
在 ES5 中主要是通過(guò)構(gòu)造函數(shù)方式和原型方式來(lái)定義一個(gè)類(lèi),但是在 ES6 新引入了 class 關(guān)鍵字,使之具有了正式類(lèi)的能力,類(lèi)(class)是ECMAScript 中新的基礎(chǔ)性語(yǔ)法糖結(jié)構(gòu)。雖然 ES6 類(lèi)表面上看起來(lái)可以支持正式的面向?qū)ο缶幊?,但?shí)際上它背后使用的仍然是原型和構(gòu)造函數(shù)的概念。
使用 ES5 定義一個(gè)類(lèi)并調(diào)用
function Person(name, age, job) {
this.name = "Totora";
this.age = 19;
this.job = "student";
this.sayName = function() {
console.log(this.name);
};
}
let person = new Person();
person.sayName();
使用 ES6 定義一個(gè)類(lèi)并調(diào)用
ES6中有兩種定義類(lèi)的方式:類(lèi)聲明和類(lèi)表達(dá)式
class Person {
constructor() {
this.name = "Totora";
this.age = 19;
this.job = "student";
}
sayName() {
console.log(this.name);
}
}
let person = new Person();
person.sayName();
//當(dāng)我們使用typeof檢測(cè)Person的類(lèi)型時(shí):
console.log(typeof Person); //function,它的本質(zhì)仍然是函數(shù)
在調(diào)用類(lèi)時(shí),不管是ES5還是ES6,都必須使用new操作符來(lái)進(jìn)行調(diào)用,不可以直接執(zhí)行。
兩者區(qū)別在于:
ES5這樣調(diào)用不會(huì)報(bào)錯(cuò),可以正常執(zhí)行(因?yàn)镋S5中的類(lèi)和普通函數(shù)幾乎沒(méi)有本質(zhì)上的區(qū)別)
function Person(name, age, job) {
this.name = "Totora";
this.age = 19;
this.job = "student";
this.sayName = function() {
console.log(this.name);
};
}
let person = Person();
console.log(person); //undefined
ES6會(huì)報(bào)錯(cuò)
class Person {
constructor() {
this.name = "Totora";
this.age = 19;
this.job = "student";
}
sayName() {
console.log(this.name);
}
}
let person =Person();
console.log(person);
person.sayName(); //Class constructor Person cannot be invoked without 'new'
變量提升
通過(guò)以下對(duì)比可以發(fā)現(xiàn),當(dāng)用class聲明類(lèi)執(zhí)行時(shí)會(huì)報(bào)錯(cuò),說(shuō)明ES6中用class定義的類(lèi)無(wú)法實(shí)現(xiàn)變量提升。
函數(shù)受函數(shù)作用域的限制,但是類(lèi)受塊作用域的限制
//變量提升
let person = new Person()
function Person(name, age, job) {
this.name = "Totora";
this.age = 19;
this.job = "student";
this.sayName = function() {
console.log(this.name);
};
}
person.sayName(); //Totora
let person = new Person();
class Person {
constructor() {
this.name = "Totora";
this.age = 19;
this.job = "student";
}
sayName() {
console.log(this.name);
}
}
person.sayName(); // Cannot access 'Person' before initialization
class中類(lèi)的構(gòu)成
類(lèi)可以包含構(gòu)造函數(shù)方法、實(shí)例方法、獲取函數(shù)、設(shè)置函數(shù)、靜態(tài)類(lèi)的方法。但是空的類(lèi)定義照樣有效
//空類(lèi)定義
class Foo {}
//有構(gòu)造函數(shù)的類(lèi)
class Bar {
constructor() {}
}
//有獲取函數(shù)的類(lèi)
class Baz {
get myBaz() {}
}
//有靜態(tài)方法的類(lèi)
class Qux {
static myQux() {}
}
class中的靜態(tài)方法
可以在類(lèi)上定義靜態(tài)方法。靜態(tài)類(lèi)成員在類(lèi)定義中使用static關(guān)鍵字作為前綴,在靜態(tài)成員中,this引用類(lèi)自身;
與原型成員類(lèi)似,靜態(tài)成員每個(gè)類(lèi)上只能有一個(gè);
static聲明的靜態(tài)屬性和方法都可以被子類(lèi)繼承。
class Person {
constructor() {
//添加到this的所有內(nèi)容都會(huì)存在于不同的實(shí)例上
this.locate = () => console.log('instance', this);
}
//定義在類(lèi)的原型對(duì)象上
locate() {
console.log('prototype', this);
}
//定義在類(lèi)本身上
static locate() {
console.log('class', this);
}
}
let p = new Person();
p.locate(); //instance Person { locate: [Function (anonymous)] }
Person.prototype.locate(); //prototype {}
Person.locate(); //class [class Person]
class Person {
static name() {
this.job(); //此處的this指向類(lèi)
}
static job() {
console.log('Totora'); //不會(huì)出現(xiàn)在實(shí)例中
}
job() {
console.log('student');
}
}
Person.name(); //Totora
繼承
ES5中的繼承實(shí)質(zhì)上是先創(chuàng)建子類(lèi)的實(shí)例對(duì)象,再將父類(lèi)的方法添加到this上(Parent.apply(this)),通過(guò)原型或構(gòu)造函數(shù)機(jī)制來(lái)實(shí)現(xiàn)
ES6的繼承實(shí)際上是先創(chuàng)建父類(lèi)的實(shí)例對(duì)象this,然后再用子類(lèi)的構(gòu)造函數(shù)修改this。
ES6中類(lèi)之間通過(guò)extends關(guān)鍵字,就可以繼承任何擁有[[Construct]]和原型的對(duì)象,在很大程度上,這不僅i僅可以繼承一個(gè)類(lèi),也可以繼承普通的構(gòu)造函數(shù)(保持向后兼容)
ES6中派生類(lèi)的方法可以通過(guò)super關(guān)鍵字引用它們的原型,這個(gè)關(guān)鍵字只能在派生類(lèi)中使用,而且僅限于類(lèi)的構(gòu)造函數(shù)、實(shí)例方法和靜態(tài)方法的內(nèi)部。在類(lèi)構(gòu)造函數(shù)中使用super可以調(diào)用父類(lèi)構(gòu)造函數(shù)。
//ES5中的繼承
function parent(a,b) {
this.a = a;
this.b = b;
}
function child(c) {
this.c = c;
}
parent.call(child, 1, 2); //子級(jí)來(lái)繼承父級(jí)
child.prototype = new parent(1, 2);
//ES6中的繼承
class parent {
constructor(a, b) {
this.a = a;
this.b = b;
}
parentMethods() {
return this.a + this.b
}
}
class child extends parent {
constructor(a, b, c) {
super(a, b); //通過(guò)super調(diào)用父類(lèi)
this.c = c;
}
childMethods() {
return this.c + ',' + super.parentMethods() //通過(guò)super實(shí)例化調(diào)用父類(lèi)
}
}
const point = new child(1, 2, 3);
console.log(point.childMethods());
總結(jié)
到此這篇關(guān)于ES5和ES6中類(lèi)區(qū)別的文章就介紹到這了,更多相關(guān)ES5和ES6類(lèi)的區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript工廠模式和構(gòu)造函數(shù)模式創(chuàng)建對(duì)象方法解析
本文主要對(duì)javascript工廠模式和構(gòu)造函數(shù)模式創(chuàng)建對(duì)象方法進(jìn)行解析,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2016-12-12
使用JavaScript實(shí)現(xiàn)檢測(cè)網(wǎng)頁(yè)是否為空閑狀態(tài)
最近開(kāi)發(fā)項(xiàng)目時(shí),常碰到“用戶在一定時(shí)間內(nèi)無(wú)任何操作時(shí),跳轉(zhuǎn)到某個(gè)頁(yè)面”的需求,所以本文就來(lái)使用JavaScript實(shí)現(xiàn)這一要求,需要的可以參考下2024-03-03
JavaScript–Apple設(shè)備檢測(cè)示例代碼
JavaScript–Apple設(shè)備檢測(cè)示例代碼。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
30個(gè)高逼格代碼的JavaScript高級(jí)單行代碼
這篇文章中列出了一個(gè)系列的 30 個(gè) JavaScript 單行代碼,它們?cè)谑褂?nbsp;vanilla js(≥ ES6)進(jìn)行開(kāi)發(fā)時(shí)非常有用,它們也是使用該語(yǔ)言在最新版本中為我們提供的所有功能來(lái)解決問(wèn)題的優(yōu)雅方式,將它們分為以下5大類(lèi):日期、字符串、數(shù)字、數(shù)組、工具2023-08-08
微信小程序中實(shí)現(xiàn)雙向綁定的實(shí)戰(zhàn)過(guò)程
最近在小程序的開(kāi)發(fā)過(guò)程中,需要用到雙向綁定,遇到報(bào)錯(cuò)才知道微信本身是不支持對(duì)象雙向綁定的,折騰一番找到解決方案,下面這篇文章主要給大家介紹了關(guān)于微信小程序中實(shí)現(xiàn)雙向綁定的相關(guān)資料,需要的朋友可以參考下2023-01-01
JavaScript函數(shù)式編程(Functional Programming)箭頭函數(shù)(Arrow functions)
這篇文章主要介紹了JavaScript函數(shù)式編程(Functional Programming)箭頭函數(shù)(Arrow functions)用法,結(jié)合實(shí)例形式分析了javascript函數(shù)式編程中箭頭函數(shù)相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2019-05-05
TypeScript模塊與命名空間的關(guān)系和使用方法
在TypeScript中就像在EC5中一樣,任何包含頂級(jí)import或export的文件都被認(rèn)為是一個(gè)模塊,下面這篇文章主要給大家介紹了關(guān)于如何在TypeScript使用模塊與命名空間以及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2023-03-03
ASP中Sub和Function的區(qū)別說(shuō)明
主要的區(qū)別是有返回值,一般sub是用來(lái)調(diào)用大量的內(nèi)容的時(shí)候用sub,對(duì)于需要計(jì)算并需要返回值的時(shí)候用function,多用function定義函數(shù)。2011-01-01
JavaScript 實(shí)現(xiàn)一個(gè)響應(yīng)式系統(tǒng)的解決方案
這篇文章主要介紹了JavaScript 實(shí)現(xiàn)一個(gè)響應(yīng)式系統(tǒng)的解決方案,本次示例使用Proxy實(shí)現(xiàn)數(shù)據(jù)監(jiān)控,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04

