node--require

 

 

1 node 版本

D:\demo\src>node -v
v12.3.1

2 全局对象

 

2.1 global

console.log(global);
Object [global] {
  global: [Circular],
  clearInterval: [Function: clearInterval],
  clearTimeout: [Function: clearTimeout],
  setInterval: [Function: setInterval],
  setTimeout: [Function: setTimeout] { [Symbol(util.promisify.custom)]: [Function] },
  queueMicrotask: [Function: queueMicrotask],
  clearImmediate: [Function: clearImmediate],
  setImmediate: [Function: setImmediate] {
    [Symbol(util.promisify.custom)]: [Function]
  }
}

2.2 global下的属性(例子如下)

console.log(global.console === console);
console.log(global.process === process);
console.log(global.Buffer === Buffer);
true
true
true

2.3 模块封装函数的变量

 

2.3.1 函数(当前模块包装的函数)

console.log(arguments.callee + '');
function (exports, require, module, __filename, __dirname) {
console.log(arguments.callee + '');
}

2.3.2 变量

console.log(require + ''); //导入函数
console.log(module); //模块对象
console.log(exports === this, this);
console.log(exports === module.exports, exports);
console.log(__dirname === module.path, __dirname);
console.log(__filename === module.filename, __filename);
function require(path) {
    return mod.require(path);
  }
Module {
  id: '.',
  path: 'D:\\demo\\src',
  exports: {},
  parent: null,
  filename: 'D:\\demo\\src\\demo.js',
  loaded: false,
  children: [],
  paths: [
    'D:\\demo\\src\\node_modules',
    'D:\\demo\\node_modules',
    'D:\\node_modules'
  ]
}
true {}
true {}
true D:\demo\src
true D:\demo\src\demo.js

3 上下文环境

  • 全局上下文(web)
  • 模块上下文
  • 函数上下文

4 代码执行顺序

 

4.1 同步代码优先执行

setTimeout(function () {
    console.log('定时器的任务代码执行了');
}, 0);
console.log('只是个打酱油的');
  只是个打酱油的
  定时器的任务代码执行了

4.2 综合优先级

 

4.2.1 Promise构造函数是同步执行的

4.2.2 Promise状态改变回调代码执行顺序优先定时器

4.2.3 定时器的时间先后执行(可以会远大于设定的时间)

const beg = new Date().getTime();
setTimeout(function () {
    console.log('1 task finish');
}, 1);

new Promise((resolve) => {
    setTimeout(function () {
        resolve('2 task finish');
    }, 2);
}).then((res) => {
    console.log(res);
});

new Promise((resolve) => {
    setTimeout(function () {
        resolve('3 task finish');
    }, 3);
}).then((res) => {
    console.log(res);
});

new Promise((resolve) => {
    resolve('Promise 优先定时器');
    console.log("promise constructor");
}).then((res) => {
    console.log(res);
});


setTimeout(function () {
    console.log('4 task finish');
}, 0);

let a = 1000, b= 0;
for(let i = 0; i < 2000000000; ++i){
    b = a * a + i;
}

const end = new Date().getTime();
console.log(end - beg);
promise constructor
1923
Promise 优先定时器
1 task finish
4 task finish
2 task finish
3 task finish

5 不带扩展名文件优先导入顺序

 

5.1 hierarchy

.
├── a.js
├── a.json
├── b.json
└── demo.js

5.2 ./

 

5.2.1 a.js

module.exports = "js文件优先";

5.2.2 a.json

{
  "string" : "json文件优先"
}

5.2.3 b.json

{
  "string" : "js文件不存在,json也可以"
}

5.2.4 demo.js

console.log(require('./a'));
console.log(require('./b'));

5.3 result

js文件优先
{ string: 'js文件不存在,json也可以' }

6 不指定文件的目录入口文件导入次序

 

6.1 hierarchy

.
├── a
│   ├── index.js
│   ├── m.js
│   └── package.json
├── b
│   ├── index.js
│   ├── m.js
│   └── package.json
├── c
│   └── index.js
└── demo.js

6.2 dir

 

6.2.1 ./a

 
  1. index.js
    module.exports = "index.js文件优先导入";
    
  2. package.json
    {
      "name": "a",
      "version": "1.0.0",
      "main" : "./m.js"
    }
    
  3. m.js
    module.exports = "package.json 中main属性指定的文件优先导入";
    

6.2.2 ./b

  • 和./a目录一样,只有package.json不同
{
  "name": "a",
  "version": "1.0.0"
}

6.2.3 ./demo.js

console.log(require('./a'));
console.log(require('./b'));
console.log(require('./c'));

6.3 result

package.json 中main属性指定的文件优先导入
index.js文件优先导入
没有package.json文件,index.js文件也可以

Created: 2019-12-10 周二 16:53

Validate

posted @ 2019-12-10 16:54  heidekeyi  阅读(519)  评论(0编辑  收藏  举报