码迷,mamicode.com
首页 > 其他好文 > 详细

ECMAScript 6的导入和导出:import 、export

时间:2017-08-02 23:24:25      阅读:1697      评论:0      收藏:0      [点我收藏+]

标签:let   new   梦想   comm   默认   script   cti   ***   浏览器   

一 基本语法

import { dog , person , hero } from ‘./common.js‘;
import { hello,Plane } from ‘./common.js‘;
import { car , rain } from ‘./common.js‘;
import shrimp from ‘./common.js‘;

console.log(lottery);

console.log(dog,person,hero);

hello();

let plane = new Plane(‘波音787梦想飞机‘);
plane.fly();

console.log(car);

rain();

console.log(shrimp);

 

/****** 单个导出  ******/

// 导出变量

export var dog = ‘沙皮狗‘;
export let person = ‘Leonardo Da Vinci‘;
export const hero = ‘常山赵子龙‘;

//导出函数

export function hello(){
    console.log(‘hello‘);
}

// 导出类

export class Plane{
    constructor(name){
        this.name = name;
    }
    fly(){
        console.log(this.name+‘起飞‘);
    }
}

/******  批量导出  ******/

let car = ‘Ferrari 612‘;

function rain(){
    console.log(‘正在下雨‘);
}
export { car,rain };

/****** 默认导出 ******/

export default ‘雀尾螳螂虾‘;
//export default function(){};
//export default class{};

 

二 其它事项

1 一个变量、函数、类只能被导出一次。

2 一个脚本最多只能有一个默认导出语句。

3 所有浏览器都不支持导入、导出语法。需要借助babel、webpack等。

ECMAScript 6的导入和导出:import 、export

标签:let   new   梦想   comm   默认   script   cti   ***   浏览器   

原文地址:http://www.cnblogs.com/sea-breeze/p/7277100.html

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