日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

node

發(fā)布時(shí)間:2023/12/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 node 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

?

?

Table of Contents

  • 1. 全局對(duì)象
  • 2. 代碼執(zhí)行優(yōu)先級(jí)
  • 3. 模塊導(dǎo)入
  • 4. 模塊加載
    • 4.1. 文件模塊優(yōu)先級(jí)
    • 4.2. 文件夾加載優(yōu)先級(jí)
      • 4.2.1. 包(文件夾)下的入口文件優(yōu)先級(jí)
      • 4.2.2. 包加載優(yōu)先級(jí)
  • 5. 核心模塊的簡(jiǎn)單使用
    • 5.1. events

1 全局對(duì)象

  • 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] // } // }
  • console
console.log(console)// { // log: [Function: bound consoleCall], // warn: [Function: bound consoleCall], // dir: [Function: bound consoleCall], // time: [Function: bound consoleCall], // timeEnd: [Function: bound consoleCall], // timeLog: [Function: bound consoleCall], // trace: [Function: bound consoleCall], // assert: [Function: bound consoleCall], // clear: [Function: bound consoleCall], // count: [Function: bound consoleCall], // countReset: [Function: bound consoleCall], // group: [Function: bound consoleCall], // groupEnd: [Function: bound consoleCall], // table: [Function: bound consoleCall], // debug: [Function: bound consoleCall], // info: [Function: bound consoleCall], // dirxml: [Function: bound consoleCall], // error: [Function: bound consoleCall], // groupCollapsed: [Function: bound consoleCall], // Console: [Function: Console], // profile: [Function: profile], // profileEnd: [Function: profileEnd], // timeStamp: [Function: timeStamp], // context: [Function: context], // [Symbol(kBindStreamsEager)]: [Function: bound ], // [Symbol(kBindStreamsLazy)]: [Function: bound ], // [Symbol(kBindProperties)]: [Function: bound ], // [Symbol(kWriteToConsole)]: [Function: bound ], // [Symbol(kGetInspectOptions)]: [Function: bound ], // [Symbol(kFormatForStdout)]: [Function: bound ], // [Symbol(kFormatForStderr)]: [Function: bound ] // }
  • exports, require, module, _filename, _dirname(模塊參數(shù))
console.log(arguments.callee + '');// function (exports, require, module, __filename, __dirname) { // console.log(arguments.callee + '');

2 代碼執(zhí)行優(yōu)先級(jí)

同步代碼優(yōu)先,例子如下

// 代碼執(zhí)行優(yōu)先級(jí) setTimeout(function () {setTimeout(function () {console.log('time out');}, 0);new Promise(resolve => {setTimeout(function () {console.log('start in Promise');}, 1);console.log('beg');resolve();console.log('end');setTimeout(function () {console.log('end in Promise');}, 0);}).then(() => {console.log('finish');});console.log('不要調(diào)皮'); }, 100);// beg // end // 不要調(diào)皮 // finish // time out // start in Promise // end in Promise

3 模塊導(dǎo)入

同步導(dǎo)入,module.exports = exports ==> true

{ test } ? tree . ├── index.js └── test.js ./index.js
console.log("index.js"); ./test.js
require('./index.js'); console.log('test.js');

output: 導(dǎo)入之后才會(huì)繼續(xù)執(zhí)行代碼

index.js test.js

4 模塊加載

4.1 文件模塊優(yōu)先級(jí)

這里只考慮 .js .json文件路徑加載

文件結(jié)構(gòu)
. ├── a.js ├── a.json ├── b.json └── test.js a.js
module.exports = "js文件優(yōu)先"; a.json
{"s": "json文件優(yōu)先" } b.json
{"main" : "json 文件也支持省略擴(kuò)展名的方式加載" } test.js
// 測(cè)試js文件先加載 console.log(require('./a')); // 證明json也可以加載 console.log(require('./b')); output
默認(rèn)文件加載js先于json文件
js文件優(yōu)先 { main: 'json 文件也支持省略擴(kuò)展名的方式加載' }

4.2 文件夾加載優(yōu)先級(jí)

4.2.1 包(文件夾)下的入口文件優(yōu)先級(jí)

  • 文件結(jié)構(gòu)
    . ├── a │?? ├── index.js │?? ├── m.js │?? └── package.json ├── b │?? ├── index.js │?? └── package.json ├── c │?? └── index.js └── test.js
  • ./a
    index.js
    module.exports = "index.js文件優(yōu)先"; m.js
    module.exports = "package.json文件優(yōu)先"; package.json
    {"name": "a","version": "1.0.0","main" : "m.js" }
  • ./b
    index.js
    module.exports = "./b/index.js文件優(yōu)先"; package.json
    {"name": "a","version": "1.0.0" }
  • ./c
    index.js
    module.exports = "index.js支持默認(rèn)加載";
  • ./test.js
    // 優(yōu)先加載packagae.json文件 console.log(require('./a')); // packagae.json中main屬性指定加載某文件 console.log(require('./b')); // index.js也支持默認(rèn)加載 console.log(require('./c'));
  • output

    package.json文件中有main優(yōu)先于index.js文件

    package.json文件優(yōu)先 ./b/index.js文件優(yōu)先 index.js支持默認(rèn)加載
  • 4.2.2 包加載優(yōu)先級(jí)

  • 路徑加載
    文件結(jié)構(gòu)
    . ├── fs │?? └── index.js └── test.js ./fs/index.js
    module.exports = "路徑加載優(yōu)先級(jí)高"; ./fs/test.js
    // 加載核心模塊 console.log(require('fs')); // 第三方模塊 console.log(require('./fs')); output

    路徑加載優(yōu)先級(jí)高于核心模塊

    {appendFile: [Function: appendFile],appendFileSync: [Function: appendFileSync],access: [Function: access],accessSync: [Function: accessSync],chown: [Function: chown],promises: [Getter]..........//還有很多 } 路徑加載優(yōu)先級(jí)高
  • 非路徑加載
    文件結(jié)構(gòu)
    . ├── node_modules │?? ├── fs │?? │?? └── index.js │?? └── tts │?? └── index.js └── test.js ./nodenodules./fs/index.js
    module.exports = "./node_nodules./fs"; ./nodenodules./tts/index.js
    module.exports = "./node_nodules./tts"; ./test.js
    // 判斷第三方模塊和核心模塊的優(yōu)先級(jí) console.log(require('fs')); // 第三方模塊可以加載 console.log(require('tts')); output
    核心模塊加載優(yōu)先于第三方模塊(nodemodules)
    {appendFile: [Function: appendFile],appendFileSync: [Function: appendFileSync],access: [Function: access],......//很多promises: [Getter] } ./node_nodules./tts
  • 第三方模塊查找過(guò)程(非路徑查找)
    文件結(jié)構(gòu)
    . ├── a │?? ├── b │?? │?? ├── c │?? │?? │?? ├── node_modules │?? │?? │?? │?? └── tts1 │?? │?? │?? │?? └── index.js │?? │?? │?? └── t.js │?? │?? └── node_modules │?? │?? └── tts2 │?? │?? └── index.js │?? └── node_modules │?? └── tts3 │?? └── index.js └── node_modules└── tts4└── index.js module.paths 中列表的順序查找
    遞歸向上級(jí)目錄查找
    ['C:\\a\\b\\c\\node_modules','C:\\a\\b\\node_modules','C:\\a\\node_modules','C:\\node_modules' ]
  • 5 核心模塊的簡(jiǎn)單使用

    5.1 events

    繼承了事件類,自身不用實(shí)現(xiàn)事件類

    const events = require('events');class Teacher extends events {constructor(sec = 2000) {super();this.sec = sec;this.doing();}doing() {let cnt = 0;setInterval(() => {++cnt;this.emit('class', {cnt});}, this.sec);} }const t = new Teacher(); t.on('class', function (args) {console.log('time to class:', args.cnt); });

    Created: 2019-06-26 周三 09:31

    Validate

    轉(zhuǎn)載于:https://www.cnblogs.com/heidekeyi/p/11075506.html

    總結(jié)

    以上是生活随笔為你收集整理的node的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。