AV免费播放一区二区三区_亚洲综合网第三页_日本一 中文字幕久久综合伊人_麻豆久久久9性大片_日韩av

ES6 模塊知識點總結(jié)

2020-8-26    前端達人

模塊化 export 和 import

import 導(dǎo)入模塊、export 導(dǎo)出模塊
可以直接在任何變量或者函數(shù)前面加上一個 export 關(guān)鍵字,就可以將它導(dǎo)出。
在一個文件中:

export const sqrt = Math.sqrt; export function square(x) { return x * x; } export function diag(x, y) { return sqrt(square(x) + square(y)); }  
    然后在另一個文件中這樣引用:
import { square, diag } from 'lib'; console.log(square(11)); // 121 console.log(diag(4, 3));  

總結(jié)

//mod.js // 第一種模塊導(dǎo)出的書寫方式(一個個的導(dǎo)出) // 導(dǎo)出普通值 export let a = 12; export let b = 5; // 導(dǎo)出json export let json = { a, b }; // 導(dǎo)出函數(shù) export let show = function(){ return 'welcome'; }; // 導(dǎo)出類 export class Person{ constructor(){ this.name = 'jam'; } showName(){ return this.name; } } //index.js //導(dǎo)出模塊如果用default了,引入的時候直接用,若沒有用default,引入的時候可以用{}的形式 // 導(dǎo)入模塊的方式 import { a, b, json, show, Person } from './mod.js'; console.log(a); // 12 console.log(b); // 5 console.log(json.a); // 12 console.log(json.b); // 5 console.log(show()); // welcome console.log(new Person().showName()); // jam //mod1.js // 第二種模塊導(dǎo)出的書寫方式 let a = 12; let b = 5; let c = 10; export { a, b, c as cc // as是別名,使用的時候只能用別名,特別注意下 }; //index1.js // 導(dǎo)入模塊的方式 import { a, b, cc // cc是導(dǎo)出的,as別名 } from './mod1.js'; console.log(a); // 12 console.log(b); // 5 console.log(cc); // 10 //mod2.js // 第三種模塊導(dǎo)出的書寫方式 ---> default // default方式的優(yōu)點,import無需知道變量名,就可以直接使用,如下 // 每個模塊只允許一個默認出口 var name = 'jam'; var age = '28'; export default { name, age, default(){ console.log('welcome to es6 module of default...'); }, getName(){ return 'bb'; }, getAge(){ return 2; } }; //index2.js // 導(dǎo)入模塊的方式 import mainAttr from './mod2.js'; var str = ' '; // 直接調(diào)用 console.log(`我的英文名是:${mainAttr.name}我的年齡是${mainAttr.age}`); mainAttr.default(); // welcome to es6 module of default... console.log(mainAttr.getName()); // bb console.log(mainAttr.getAge()); // 2 //mod3.js var name = 'jam'; var age = '28'; export function getName(){ return name; }; export function getAge(){ return age; }; //index3.js // 導(dǎo)入模塊的方式 import * as fn from './mod3.js'; // 直接調(diào)用 console.log(fn.getName()); // 


日歷

鏈接

個人資料

藍藍設(shè)計的小編 http://www.hglv.net

存檔

环江| 沁源县| 鹤岗市| 鄂尔多斯市| 大安市| 扶沟县| 芮城县| 自治县| 兴仁县| 绍兴市| 鹿泉市| 嘉义市| 正镶白旗| 洪江市| 厦门市| 健康| 沂南县| 顺义区| 苏州市| 阳朔县| 万全县| 神木县| 漠河县| 广安市| 孟连| 通辽市| 凤阳县| 五台县| 临洮县| 大足县| 大足县| 水富县| 全州县| 黄石市| 新绛县| 墨竹工卡县| 阳城县| 南阳市| 镶黄旗| 辉县市| 石屏县|