JavaScript import/export

定义模块

ES6之后,使用模块语法(import/export)时,每个文件都会成为它自己的模块,带有一个私有全名空间。顶层的函数和变量不会污染全局全名空间。要为其他模块暴露函数,类,和变量以便import的话,可以用export关键字。

// not exported
function somethingPrivate() {
    console.log('TOP SECRET')
}


export const PI = 3.14;

export function doSomething() {
    console.log('Hello from a module!')
}

function doSomethingElse(){ 
    console.log("Something else")
}

export {doSomethingElse}

export class MyClass {
    test() {}
}

import整个模块

import * as test from './test'

test.doSomething()

./test表示test.js的路径。

import命名成员

import {doSomething, MyClass, PI} from './test'

doSomething()

const mine = new MyClass()
mine.test()

console.log(PI)

语法


import defaultMember from 'module';


import { memberA, memberB, ... } from 'module';


import * as module from 'module';


import { memberA as a, memberB, ... } from 'module';


import defaultMember, * as module from 'module';


import defaultMember, { moduleA, ... } from 'module';


import 'module';
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值