Skip to content

Commit

Permalink
es6 commonjs
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed May 28, 2020
1 parent ba3e438 commit 8a572c6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
15 changes: 15 additions & 0 deletions es6 commonjs区别/commonjs.js
Original file line number Diff line number Diff line change
@@ -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')
7 changes: 7 additions & 0 deletions es6 commonjs区别/es6.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export let counter = 3

export function addCount() {
counter ++
}

console.log('es6')
22 changes: 22 additions & 0 deletions es6 commonjs区别/index.mjs
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="./promise2.js"></script>
</body>
</html>
46 changes: 46 additions & 0 deletions promise2.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 8a572c6

Please sign in to comment.