技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁(yè) > 教程 > 服務(wù)器類 >

ES6入門教程之Class和Module詳解

來(lái)源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2017-10-23 18:34┆點(diǎn)擊:

ES6引入了Class(類)這個(gè)概念,作為對(duì)象的模板。通過(guò)class關(guān)鍵字,可以定義類。

// 定義類 class Point() { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } var point = new Point(2, 3); point.toString(); //(2, 3)

在上面的代碼片段里,先是定義了一個(gè)Point類,里面還有一個(gè)constructor函數(shù),這就是構(gòu)造函數(shù)。而this關(guān)鍵字則代表實(shí)例對(duì)象。

Class之間可以通過(guò)extends關(guān)鍵字實(shí)現(xiàn)繼承

Class ColorPoint extends Point { constructor(x, y, color) { super(x, y); //等同于super.constructor(x, y) this.color = color; } toString() { return this.color + '' + super(); } }

二、Module的基本用法

1>、export和import

ES6實(shí)現(xiàn)了模塊功能,視圖解決JavaScript代碼的依賴和部署上的問(wèn)題,取代現(xiàn)有的commonJS和AMD規(guī)范,成為瀏覽器和服務(wù)器通用的模塊解決方案。

模塊的功能有兩個(gè)關(guān)鍵字: export和import。export用于用戶自定義模塊。規(guī)定對(duì)外接口;import用于輸入其他模塊的功能,同時(shí)創(chuàng)建命名空間(namespace),防止函數(shù)名沖突。

ES6允許將獨(dú)立的JS文件作為模塊,也就是說(shuō),允許一個(gè)JavaScript腳本文件調(diào)用另一個(gè)腳本文件。最簡(jiǎn)單的模塊就是一個(gè)JS文件,里面使用export關(guān)鍵字輸出變量。

//profile.js export var firstName = "Pandora"; export var lastName = "G.Dragon"; export var year = 1973; //export還有下面這種寫(xiě)法,兩者是等價(jià)的 var firstName = "Pandora"; var lastName = "G.Dragon"; var year = 1973; export({firstName, lastName, year});

使用export定義模塊之后,其他JS文件就可以通過(guò)import關(guān)鍵字加載這個(gè)模塊(文件)了。加載方式如下:

import {firstName, lastName, year} from './profile'; function setHeader(element) { element.textContent = firstName + '' + lastName; }

上面的代碼片段中,使用了import關(guān)鍵字接受一個(gè)對(duì)象——用“{ }”表示。里面指定了要從其他模塊中導(dǎo)入的變量。大括號(hào)里面的變量名必須與被導(dǎo)入模塊對(duì)外接口的名稱相同。

但是,如果要給輸入的屬性和方法重新取一個(gè)名字,import語(yǔ)句要寫(xiě)成下面這樣。

import {someMethod, another as newName} from './exporter';

2>、模塊的整體加載

export關(guān)鍵字除了輸出變量,還可以輸出方法或類(class)??纯聪旅娲a:

// circle.js // 方法-1: 返回圓的面積 export function area(radius) { return Math.PI * radius * radius; } // 方法-2: 返回圓的周長(zhǎng) export function circumference(radius) { return 2 * Mathi.PI * radius; }

下面,定義一個(gè)main.js文件引用上面的模塊。

// mian.js import {area, circumference} from 'circle'; console.log("圓面積: " + area(4)); console.log("圓周長(zhǎng): " + circumference(14));

上面的寫(xiě)法是逐一制定要導(dǎo)入的方法。但是還有另外一種寫(xiě)法,就是使用module關(guān)鍵字,整體導(dǎo)入。

// main.js module circle from 'circle'; console.log("圓面積: " + circle.area(4)); console.log("圓周長(zhǎng): " + circle.circumference(14));

module關(guān)鍵字后面跟著一個(gè)變量,表示導(dǎo)入的模塊定義在該變量上。

3>、export default語(yǔ)句

如果不想為某個(gè)屬性或方法制定輸入的名稱,可以使用export default語(yǔ)句。

// export-default.js export default function foo() { // foo()就是這個(gè)模塊的默認(rèn)方法 console.log('foo'); }

當(dāng)在其它模塊中導(dǎo)入該模塊時(shí),import語(yǔ)句可以為默認(rèn)方法指定任意名字。

// import-default.js import customName from './export-default'; customName(); //'foo'

顯然,一個(gè)模塊只能由一個(gè)默認(rèn)方法。
對(duì)于默認(rèn)屬性,則是直接定義在export default后面即可。如下代碼所示:

export default 42;

三、模塊的繼承

模塊之間是可以繼承的。

現(xiàn)在,假設(shè)一個(gè)circlePlus模塊繼承了circle模塊。代碼如下:

//circleplus.js export * from 'circle'; // "export *"表示輸出circle模塊的所有屬性和方法 export var e = 2.71828; export default function(x) { return Math.exp( x ); }

這時(shí),可以對(duì)cicle中的屬性和方法改名后再輸出。

export {area as circleArea } from 'circle';

加載模塊的寫(xiě)法如下:

//main.js module math from 'circleplus'; import exp from "circleplus"; // "import exp"表示將circleplus模塊的默認(rèn)方法加載為exp方法。 console.log( exp(math.pi) );