零、区别

1、require/exports 是 CommonJS 的标准,适用范围如 Node.js

2、import/export 是 ES6 的标准,适用范围如 React

一、间接获取对象

(1)require/exports

module.js

exports.name = "colin";
exports.sayHello = function() {
console.log("hello");
};
方法一 间接

getModule.js

var myModule = require('./module');
console.log(myModule.name);
myModule.sayHello();
方法二 直接

getModule.js

var { name, sayHello } = require('./module');
console.log(name);
sayHello();
方法三 别名

getModule.js

var { name: name_2, sayHello: sayHello_2 } = require('./module');
console.log(name_2);
sayHello_2();

(2)import/export

module.js

export const name = 'colin';
export function sayHello(){
console.log("hello");
}
方法一 间接 (不可用)

getModule.js

import myModule from './module'
console.log(myModule.name);
myModule.sayHello();

报错如下:

"export 'default' (imported as 'myModule') was not found in '@cp/utils/format'

方法二 直接

getModule.js

import { name, sayHello } from './module'
console.log(name);
sayHello();
方法三 别名

getModule.js

import { name as name_2, sayHello as sayHello_2 } from './module'
console.log(name_2);
sayHello_2();

二、直接获取对象

(1)require/exports

module.js

var name = "colin";
module.exports = name;

getModule.js

var name = require('./module');
console.log(name);

(2)import/export

module.js

export default name = 'colin';

getModule.js

import name from './module'
console.log(name);

三、拓展

上面介绍的都是加载 js 文件,但是也可以加载 json 文件。

下面以 require/exports 为例 (不需要加上 exports 即可直接导出)

module.json

{
"name": "a",
"age": 18
}

getModule.js

const name = require('./module');
const {name, age} = require('./module');

require/exports 和 import/export 区别的更多相关文章

  1. require/exports 与 import/export 的区别?

    文章作者:寸志链接:https://www.zhihu.com/question/56820346/answer/150724784来源:知乎 遵循的模块化规范不一样 模块化规范:即为 JavaScr ...

  2. 简单介绍export default,module.exports与import,require的区别联系

    他们都是成对使用的,不能乱用: module.exports 和 exports是属于CommonJS模块规范,对应---> require属于CommonJS模块规范 export 和 exp ...

  3. export,export default,module.exports,import,require之间的区别和关联

    module.exports Node 应用由模块组成,采用 CommonJS 模块规范.根据这个规范,每个文件就是一个模块,有自己的作用域.在这些文件里面定义的变量.函数.类,都是私有的,对外不可见 ...

  4. 探讨ES6的import export default 和CommonJS的require module.exports

    今天来扒一扒在node和ES6中的module,主要是为了区分node和ES6中的不同意义,避免概念上的混淆,同时也分享一下,自己在这个坑里获得的心得. 在ES6之前 模块的概念是在ES6发布之前就出 ...

  5. js中的require、define、export、import【转】

    原文链接:https://www.cnblogs.com/libin-1/p/7127481.html 为什么有模块概念 理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块. ...

  6. Javascript在使用import 与export 区别及使用

    一.import与export的用法 1.import的几种用法 import defautName from 'modules.js'; import { export } from 'module ...

  7. export,export default和import的区别以及用法

    首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和import export用于对外输出本模块(一个文件可以理解为一个模块)变量的接口 ...

  8. ES6常用语法简介import export

    ES6常用语法简介import export let与var用法区别 //var var a = []; for (var i = 0; i < 10; i++) { a[i] = functi ...

  9. 关于node中 require 和 ES6中export 、export default的总结

    nodejs中 require 方法的加载规则 方法的加载规则 1. 优先从缓存中加载 2. 核心模块 3. 路径形式的模块 4. 第三方模块 一.优先从缓存中加载 main.js:执行加载a.js模 ...

随机推荐

  1. Socket层实现系列 — 睡眠驱动的同步等待

    主要内容:Socket的同步等待机制,connect和accept等待的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 概述 socket上定义了 ...

  2. Dynamics CRM EntityCollection 根据实体中的某个字段为依据去除重复数据

    CRM中通过QueryExpression查询出了一个EntityCollection集,但有时会存在重复数据,QueryExpression中有个属性distinct,只要设置为true就能过滤 ...

  3. Socket编程实践(3) --Socket API

    socket函数 #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, ...

  4. DesignModeler&nbsp;GestureRecgin…

    DesignModeler : 设计模式     GestureRecginzer:手势识别 作者:韩俊强 原创版权地址:http://blog.sina.com.cn/s/blog_814ecfa9 ...

  5. Unity访问Access数据库

    首先,准备工作: 创建一个Access 数据库,命名AccessTest.accdb,添加一些数据用于测试 准备System.Data.dll与System.EnterpriseServices.dl ...

  6. (一)UI设计的一些常识

    一.概述 新版本的Xcode似乎框架不明示. UIView:屏幕上看得见摸得着的东西.视图.控件.组件. UIView是一个容器,能容纳其他UIView. UIViewController用来控制UI ...

  7. C++多重继承与虚拟继承

    本文只是粗浅讨论一下C++中的多重继承和虚拟继承. 多重继承中的构造函数和析构函数调用次序 我们先来看一下简单的例子: #include <iostream> using namespac ...

  8. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  9. Struts源码之OgnlValueStack

    public class OgnlValueStack implements Serializable, ValueStack, ClearableValueStack, MemberAccessVa ...

  10. 史上最简单的C语言链表实现,没有之一

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define NR(x) (sizeof(x ...