码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript import/export

时间:2016-11-17 20:50:38      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:console   模块   ++   cti   from   code   key   private   class   

定义模块

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‘;

JavaScript import/export

标签:console   模块   ++   cti   from   code   key   private   class   

原文地址:http://blog.csdn.net/cuit/article/details/53204147

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!