es6 import export 与 node 中的module.exports exports
1.export
a.export 变量
export var name = 'jack';
export var age = 18;
//等同于
var name = 'jack';
var age = 18;
export {name,age};
a.export 方法
export function sayHello(){
console.log('hello world!');
}
//等同于
function sayHello(){
console.log('hello world!');
}
export {sayHello};
2.import
import 与 export 正好相反;
import {name,age} from './exportTest.js';
console.log(name);
console.log(age);
import {sayHello} from './exportTest.js';
sayHello();
3.default
默认导出的模块,每个js文件中只能有一个export default 。
正是因为export default命令其实只是输出一个叫做default的变量,所以它后面不能跟变量声明语句。
// 正确
export var a = ; // 正确
var a = ;
export default a; // 错误
export default var a = ;
export default 可以导出一个方法:
export default function test() {
this.name = "jack";
};
由于export default,在import 的时候可以为其指定任意名字,因为default 本身是匿名的。
//正确
import test from './exportTest.js'; //正确
import myTest from './exportTest.js'; //正确
import hello from './exportTest.js';
4. module.exports 与 exports
module.exports ,exports 为node.js 的方法, module.exports 重要用于导出 function Parent(){} 这种构造函数,在require中可以直接new 该对象;
exports 主要为导出实例化对象。
es6 import export 与 node 中的module.exports exports的更多相关文章
- [转] ES6 import/export:模块导入导出方式
export导出语法 // default exports export default 42; export default {}; export default []; export defaul ...
- 关于es6 import export的学习随笔
记得之前的一次面试中,有个面试官问了我关于es6导入和导出的一些知识点,可惜当时对这方面没在意,只知道每次机械的import和export,也不知道为啥要这样用,现在静下心来,好好的把这块看了下,顺便 ...
- ES6 import export
import import './module1.js'; (无对象导入) import d from './module1.js'; (导入默认对象) import {Employee, getEm ...
- es6 import export 引入导出变量方式
var testdata='sdfkshdf'; //export testdata;//err export {testdata as ms}; export var firstName = 'Mi ...
- node中的require和exports
http://cnodejs.org/topic/4f16442ccae1f4aa270010e9
- 探讨ES6的import export default 和CommonJS的require module.exports
今天来扒一扒在node和ES6中的module,主要是为了区分node和ES6中的不同意义,避免概念上的混淆,同时也分享一下,自己在这个坑里获得的心得. 在ES6之前 模块的概念是在ES6发布之前就出 ...
- require/exports 与 import/export 的区别?
文章作者:寸志链接:https://www.zhihu.com/question/56820346/answer/150724784来源:知乎 遵循的模块化规范不一样 模块化规范:即为 JavaScr ...
- ES6学习笔记(十九)Module 的语法-export和import
1.概述 历史上,JavaScript 一直没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来.其他语言都有这项功能,比如 Ruby 的require.Pyt ...
- 关于node中 require 和 ES6中export 、export default的总结
nodejs中 require 方法的加载规则 方法的加载规则 1. 优先从缓存中加载 2. 核心模块 3. 路径形式的模块 4. 第三方模块 一.优先从缓存中加载 main.js:执行加载a.js模 ...
随机推荐
- 滚动轮播效果,.net 没得混看来只能去写js 了
<!DOCTYPE html> <html> <head> <title> 滚动图片 </title> <style type=&qu ...
- Android实现文章+评论(MVP,RxJava,Dagger2,ButterKnife)
简介 这个项目主要有两个功能,一个加载网页/文章,另一个用来显示评论.并应用了MVP模式,Dagger2.RxJava.ButterKnife等开源框架.效果图如下: 结构 首先来看一下布局文件: & ...
- java 关键字 assert的学习
之前在学习java源码时,发现了assert这个不常用的关键字.下面直接来介绍下这个关键字的使用. assert是什么? 它是jdk1.4之后新增加的关键字,没了. assert的作用是什么? ass ...
- CheckBox 半选中状态
<input type='checkbox' />可以半选中,这个特性,很多浏览器都支持,包括Firefox,Chrome和IE 用 input.indeterminate 这个属性来获取 ...
- javascript 创建 div
纯JAVASCRIPPT创建 (1):document.getElementById("要创建DIV位置的ID").innerHTML='<div>div里面的 ...
- 【转】PowerShell 连接SQL Server 数据库 - ADO.NET
转至:http://www.pstips.net/connect-sql-database.html PowerShell 通过ADO.NET连接SQL Server数据库,并执行SQL脚本.工作中整 ...
- iOS 图片轮播图(自动滚动)
iOS 图片轮播图(自动滚动) #import "DDViewController.h" #define DDImageCount 5 @interface DDViewContr ...
- Lucene热词显示并选择
利用Jquery easyui里的autocomplete(1.10.0版本) 的异步请求(remot.html) 添加引用 <script src="~/Scripts/jquery ...
- Java数据结构与算法之---求两个数的最大公约数(欧几里得算法)
一个简单的小算法来获取两个数的最大公约数, public class Test { public static void main(String[] args) { long result = gcd ...
- 为什么导入数据库要加入set names utf-8
Repinted:http://blog.csdn.NET/class1/archive/2006/12/30/1469298.aspx 为了让你的网页能在更多的服务器上正常地显示,还是加上" ...