node

?

?

?

Table of Contents

  • 1. 全局對象
  • 2. 代碼執行優先級
  • 3. 模塊導入
  • 4. 模塊加載
    • 4.1. 文件模塊優先級
    • 4.2. 文件夾加載優先級
      • 4.2.1. 包(文件夾)下的入口文件優先級
      • 4.2.2. 包加載優先級
  • 5. 核心模塊的簡單使用
    • 5.1. events

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]
//     }
// }
  • 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(模塊參數)
console.log(arguments.callee + '');// function (exports, require, module, __filename, __dirname) {
//     console.log(arguments.callee + '');

2 代碼執行優先級

同步代碼優先,例子如下

// 代碼執行優先級
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('不要調皮');
}, 100);// beg
// end
// 不要調皮
// finish
// time out
// start in Promise
// end in Promise

3 模塊導入

同步導入,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: 導入之后才會繼續執行代碼

index.js
test.js

4 模塊加載

4.1 文件模塊優先級

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

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

4.2 文件夾加載優先級

4.2.1 包(文件夾)下的入口文件優先級

  1. 文件結構
    .
    ├── a
    │?? ├── index.js
    │?? ├── m.js
    │?? └── package.json
    ├── b
    │?? ├── index.js
    │?? └── package.json
    ├── c
    │?? └── index.js
    └── test.js
    
  2. ./a
    index.js
    module.exports = "index.js文件優先";
    
    m.js
    module.exports = "package.json文件優先";
    
    package.json
    {"name": "a","version": "1.0.0","main" : "m.js"
    }
    
  3. ./b
    index.js
    module.exports = "./b/index.js文件優先";
    
    package.json
    {"name": "a","version": "1.0.0"
    }
    
  4. ./c
    index.js
    module.exports = "index.js支持默認加載";
    
  5. ./test.js
    // 優先加載packagae.json文件
    console.log(require('./a'));
    // packagae.json中main屬性指定加載某文件
    console.log(require('./b'));
    // index.js也支持默認加載
    console.log(require('./c'));
    
  6. output

    package.json文件中有main優先于index.js文件

    package.json文件優先
    ./b/index.js文件優先
    index.js支持默認加載
    

4.2.2 包加載優先級

  1. 路徑加載
    文件結構
    .
    ├── fs
    │?? └── index.js
    └── test.js
    
    ./fs/index.js
    module.exports = "路徑加載優先級高";
    
    ./fs/test.js
    // 加載核心模塊
    console.log(require('fs'));
    // 第三方模塊
    console.log(require('./fs'));
    
    output

    路徑加載優先級高于核心模塊

    {appendFile: [Function: appendFile],appendFileSync: [Function: appendFileSync],access: [Function: access],accessSync: [Function: accessSync],chown: [Function: chown],promises: [Getter]..........//還有很多
    }
    路徑加載優先級高
    
  2. 非路徑加載
    文件結構
    .
    ├── 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
    // 判斷第三方模塊和核心模塊的優先級
    console.log(require('fs'));
    // 第三方模塊可以加載
    console.log(require('tts'));
    
    output
    核心模塊加載優先于第三方模塊(nodemodules)
    {appendFile: [Function: appendFile],appendFileSync: [Function: appendFileSync],access: [Function: access],......//很多promises: [Getter]
    }
    ./node_nodules./tts
    
  3. 第三方模塊查找過程(非路徑查找)
    文件結構
    .
    ├── 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 中列表的順序查找
    遞歸向上級目錄查找
    ['C:\\a\\b\\c\\node_modules','C:\\a\\b\\node_modules','C:\\a\\node_modules','C:\\node_modules'
    ]
    

5 核心模塊的簡單使用

5.1 events

繼承了事件類,自身不用實現事件類

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

轉載于:https://www.cnblogs.com/heidekeyi/p/11075506.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/248666.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/248666.shtml
英文地址,請注明出處:http://en.pswp.cn/news/248666.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

一個關于WCF調用遠程鏈接返回405錯誤不允許使用此方法的問題

最近在調試WCF的接口時一直返回“405不允許使用此方法”,這個問題困擾了大半天,網上查了各種辦法,但是每個人遇到的問題不同還是不能解決。 最后無意之中發現問題所在,記錄一下幫助后面的同學解決問題。 WCF遠程方法會配置屬性Web…

PHP從零開始--循環數組

一、循環 1.1單層for循環 1.1.1基礎語法 for(初識變量;結束范圍;累加/累減){ 重復執行的代碼 } 1、 先初識化變量$i 2、 $i<100表達式進行判斷 3、 跳入循環&#xff0c;執行重復代碼 4、 累加或者累加 5、 再進行$i<100表達式判斷 6、 再跳入循環&#xff0c;執行重復…

Spring Cloud(F版)搭建高可用服務注冊中心

上一篇文章【Spring Cloud搭建注冊中心】成功搭建了一個Eureka Server服務注冊中心&#xff0c;不過相信細心的朋友都會發現&#xff0c;這個服務注冊中心是一個單節點服務注冊中心&#xff0c;萬一發生故障或者服務器宕機&#xff0c;那所有的服務可就不能使用了&#xff0c;這…

Python(60)_閉包

1 、閉包的概念 #-*-coding:utf-8-*- 1、閉包&#xff1a;內部函數調用外部函數的變量def outer():a 1def inner():print(a)print(inner.__closure__) outer() print(outer.__closure__) 2 閉包的使用 #-*-coding:utf-8-*- 1、閉包&#xff1a;內部函數調用外部函數的變量 …

PHP從零開始--錯誤處理函數

一、錯誤處理 1.1錯誤種類 1.1.1Notices 比如沒有定義變量確使用了會報notice錯誤&#xff0c;只是提醒注意&#xff0c;不影響后續代碼執行 1.1.2Warnings 這是警告錯誤&#xff0c;比如include引入一個并不存在的文件&#xff0c;不影響后續代碼執行 1.1.3Fatal Erro…

第四單元博客總結——暨OO課程總結

第四單元博客總結——暨OO課程總結 第四單元架構設計 第一次UML作業 簡單陳述 第一次作業較為簡單&#xff0c;只需要實現查詢功能&#xff0c;并在查詢的同時考慮到性能問題&#xff0c;即我簡單的將每一次查詢的結果以及遞歸的上層結果都存儲下來&#xff0c;使用一個Boolean…

兩列布局:6種方法

面試過程中總會文檔兩列布局&#xff0c;左邊等寬&#xff0c;右邊自適應幾種方法&#xff1f;以下提供6種為君解憂 <div id"wrap"><div id"left"></div><div id"right"></div> </div>需求就是左側定寬&…

PHP從零開始--數據庫

文章目錄一、 數據庫簡介1.1概念1.2命令行操作1.3連接數據庫1.4配置環境變量二、 數據庫的相關操作2.1顯示所有倉庫2.2創建倉庫2.3刪除倉庫2.4切換倉庫三、 數據表的相關操作3.1概念3.2顯示所有的數據表3.3創建數據表3.2修改字段名3.3查看表結構3.4添加字段3.5刪除字段3.6更改數…

常用SQL語句

將記錄的某一字段值設置為空&#xff08;null&#xff09;UPDATE 表名 SET 字段名NULL WHERE 條件字段名123; 更新整列為某個值UPDATE 表名 SET 字段名NULL 轉載于:https://www.cnblogs.com/zhcBlog/p/10254066.html

如何下載js類庫

https://bower.io/ 這個已經淘汰 https://learn.jquery.com/jquery-ui/environments/bower/ Web sites are made of lots of things — frameworks, libraries, assets, and utilities. Bower manages all these things for you. Keeping track of all these packages and mak…

Python 常用系統模塊整理

Python中的常用的系統模塊中部分函數等的整理 random: 隨機數sys: 系統相關os: 系統相關的subprocess: 執行新的進程multiprocessing: 進程相關threading: 線程相關pickle: 將對象轉換成二進制文件time: 時間datetime: 基本的日期和時間類型timeit: 準確測量小段代碼的執行時間…

PHP從零開始--字段修飾符數據操作SQL語言

文章目錄一、 字段修飾符1.1主鍵1.2自動增長1.3非空1.4默認值1.5外鍵二、 對數據的操作2.1增加數據2.2刪除數據2.3更新數據2.4查詢數據2.4.1查詢所有的數據2.4.2查詢指定字段2.4.3去除重復字段2.4.4where表達式詳解2.4.5分組查詢2.4.6排序三、 SQL語言3.1DML3.2DDL3.3DCL一、 字…

scrapy爬蟲框架windows下的安裝問題

windows操作系統python版本是3.6.0通過Anaconda命令conda install scrapy安裝scrapy,安裝過程中沒有問題。然后在命令行輸入命令準備新建項目時&#xff0c;輸入 scrapy startproject firstscrapy時出現了from cryptography.hazmat.bindings._openssl import ffi, libImportErr…

charles使用說明(基于mac)

1. Charles簡介 1.1 Charles 需要java的運行環境支持&#xff0c;支持Windows、Mac&#xff1b;Fiddler不支持Mac。故Charles是在Mac下常用的網絡封包截取工具。 1.2 Charles原理&#xff1a;通過將自己設置成系統的網絡訪問代理服務器&#xff0c;使得所有的網絡訪問請求都通過…

看完就懂的連表查詢

文章目錄一、表與表之間的關系1.1一對一1.2一對多1.3多對多二、 連表查詢2.1概念2.2笛卡爾積2.3內連接2.4外連接2.4.1左外連接2.4.2右外連接2.4.3全連接2.4.4navicat導入導成sql語句2.4.5練習三、 子查詢3.1概念3.2練習3.2.1查詢工資最高的員工所有信息3.2.2查詢工資比7654工資…

jpa

Transactionalpublic void testPerson() {try {Person person1 personDao.findById(1);person1.setAddress("天津");} catch (Exception e) {e.printStackTrace();}} service就這樣一個方法&#xff0c;數據庫中數據也會進行更新 將查詢出來的數據對象賦值,然后不執…

影視感悟專題---1、B站-魔獸世界代理及其它亂七八糟

影視感悟專題---1、B站-魔獸世界代理及其它亂七八糟 一、總結 一句話總結&#xff1a; 看過的東西都可以學下&#xff0c;這樣既可以學習那些東西&#xff0c;都是對自己生活學習有幫助的&#xff0c;還可以彌補自己每天學的東西的不夠 1、《美麗心靈》中的博弈論共贏理論指的啥…

三分鐘掌握PHP操作數據庫

這里寫自定義目錄標題一、 操作數據庫&#xff08;mysql&#xff09;的工具1.1命令行工具1.2navicat界面化工具1.3phpAdmin界面化工具二、 表單傳值2.1文本框和文本域傳值2.2單選框傳值2.4下拉菜單傳值三、 php連接數據庫3.1連接方式介紹3.2mysqli基礎步驟3.2.1創建連接3.2.2選…

go語言之進階篇主協程先退出導致子協程沒來得及調用

1、主協程先退出導致子協程沒來得及調用 示例&#xff1a; package mainimport ("fmt""time" )//主協程退出了&#xff0c;其它子協程也要跟著退出 func main() {go func() {i : 0for {ifmt.Println("子協程 i ", i)time.Sleep(time.Second)}}(…

Actor模型(分布式編程)

Actor的目的是為了解決分布式編程中的一系列問題。所有消息都是異步交付的&#xff0c;因此將消息發送方與接收方分開&#xff0c;正是由于這種分離&#xff0c;導致actor系統具有內在的并發性&#xff1a;可以不受限制地并行執行任何擁有輸入消息的 actor。用Actor寫的程序可以…