-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |