From 8a572c6f7360d13c9142fa8a516218d1dbf9a2ef Mon Sep 17 00:00:00 2001 From: YangJ0605 <1442122744@qq.com> Date: Thu, 28 May 2020 11:50:05 +0800 Subject: [PATCH] es6 commonjs --- .../commonjs.js" | 15 ++++++ .../es6.mjs" | 7 +++ .../index.mjs" | 22 +++++++++ index.html | 11 +++++ promise2.js | 46 +++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 "es6 commonjs\345\214\272\345\210\253/commonjs.js" create mode 100644 "es6 commonjs\345\214\272\345\210\253/es6.mjs" create mode 100644 "es6 commonjs\345\214\272\345\210\253/index.mjs" create mode 100644 index.html create mode 100644 promise2.js diff --git "a/es6 commonjs\345\214\272\345\210\253/commonjs.js" "b/es6 commonjs\345\214\272\345\210\253/commonjs.js" new file mode 100644 index 0000000..96d3dbe --- /dev/null +++ "b/es6 commonjs\345\214\272\345\210\253/commonjs.js" @@ -0,0 +1,15 @@ +var counter = 3 +var obj = {a:1} + +function addConunt() { + counter ++ + // console.log(counter) + obj.a ++ +} + +module.exports = { + counter, + addConunt, + obj +} +console.log('common') \ No newline at end of file diff --git "a/es6 commonjs\345\214\272\345\210\253/es6.mjs" "b/es6 commonjs\345\214\272\345\210\253/es6.mjs" new file mode 100644 index 0000000..25f1317 --- /dev/null +++ "b/es6 commonjs\345\214\272\345\210\253/es6.mjs" @@ -0,0 +1,7 @@ +export let counter = 3 + +export function addCount() { + counter ++ +} + +console.log('es6') \ No newline at end of file diff --git "a/es6 commonjs\345\214\272\345\210\253/index.mjs" "b/es6 commonjs\345\214\272\345\210\253/index.mjs" new file mode 100644 index 0000000..ee704e0 --- /dev/null +++ "b/es6 commonjs\345\214\272\345\210\253/index.mjs" @@ -0,0 +1,22 @@ +var common = require('./commonjs') +console.log(common.counter) +console.log(common.obj) +common.addConunt() +console.log(common.counter) +console.log(common.obj) +/** + * CommonJS模块输出的是一个值的拷贝,ES6 模块输出的是值的引用; + * CommonJS 模块是运行时加载,ES6 模块是编译时输出接口。 + * + * + * CommonJS模块规范使用require语句导入模块,module.exports导出模块,输出的是值的拷贝,模块导入的也是输出值的拷贝,也就是说,一旦输出这个值,这个值在模块内部的变化是监听不到的。 + * + * + * ES6模块的规范是使用import语句导入模块,export语句导出模块,输出的是对值的引用。ES6模块的运行机制和CommonJS不一样,遇到模块加载命令import时不去执行这个模块,只会生成一个动态的只读引用,等真的需要用到这个值时,再到模块中取值,也就是说原始值变了,那输入值也会发生变化。 + */ + +// import {counter, addCount} from './es6.mjs' +// console.log(counter) +// addCount() +// console.log(counter) + // node --experimental-modules index.mjs \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..c16a62a --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ + + + + + + Document + + + + + \ No newline at end of file diff --git a/promise2.js b/promise2.js new file mode 100644 index 0000000..215102b --- /dev/null +++ b/promise2.js @@ -0,0 +1,46 @@ +// function a(fn) { +// fn && fn() +// } +// console.log(0) +// a(() => console.log(1)) +// console.log(2) + +/* +function something() { + if(Date.now() % 2 ===1) { + console.log('当前时间为记数~') + } else { + throw new Error('偶数') + } +} + +try { + something() +} catch(err){ + console.log(err.message) +} +*/ + +// pending resolved rejected + +let p1 = new Promise((resolve,reject) => { + setTimeout(() => { + let random = Math.random() + if(random > 0.5) { + resolve(random) + } else { + reject(random) + } + },1000) +}) +console.log(p1) + +p1.then((value) => { + console.log('resolve:'+ value) +}, (reason) => { + console.log('reject:'+ reason) +}) + +// setTimeout(() => { +// console.log(p1) +// },1100)