node.js之CommonJS

 一、CommonJSphp

  咱們只是在js文件中寫下console.log('This is a test.');這句代碼,node.js執行該js文件時:java

(
  function(exports, require, module, __filename, __dirname) {
      console.log('This is a test.');  
  }  
);

  - 每一個文件是一個模塊,有本身的做用域node

  - 在模塊內部module變量表明模塊自己mysql

  - module.exports屬性表明模塊對外接口sql

 

二、require特性npm

  - module被加載時執行,加載後緩存數組

  - 一旦出現某個模塊被循環加載,就只會輸出已經執行的部分,還未執行的部分不輸出。開發中要避免兩個模塊相互加載。緩存

 

三、node.js將全局屬性和方法掛到global上less

  - CommonJsui

  - Buffer、process、console

  - timer(setTimeout、setImmediate等)

 

四、process進程

  - node啓動相關一些參數:argv、argv0、execArgv、execPath

const { argv, argv0, execArgv, execPath } = process;

argv.forEach(item => {
    console.log(item);
});

   結果:

  argv0就是argv數組的第一個值。

  

const { argv, argv0, execArgv, execPath } = process;

argv.forEach(item => {
    console.log(item);
});

console.log(argv0);
console.log(execArgv);

 

 

 

const { env } = process;
console.log(env);

  結果:

{ '#envTSLOGSHELLEXT10724': '351103568',
  '#envTSLOGsss10724': '80627808',
  '#envTSLOGTSLOG10724': '80616288',
  ALLUSERSPROFILE: 'C:\\ProgramData',
  APPDATA: 'C:\\Users\\oy\\AppData\\Roaming',
  CommonProgramFiles: 'C:\\Program Files\\Common Files',
  'CommonProgramFiles(x86)': 'C:\\Program Files (x86)\\Common Files',
  CommonProgramW6432: 'C:\\Program Files\\Common Files',
  COMPUTERNAME: 'LAPTOP-SHVUJ9TS',
  ComSpec: 'C:\\WINDOWS\\system32\\cmd.exe',
  DriverData: 'C:\\Windows\\System32\\Drivers\\DriverData',
  FPS_BROWSER_APP_PROFILE_STRING: 'Internet Explorer',
  FPS_BROWSER_USER_PROFILE_STRING: 'Default',
  HOMEDRIVE: 'C:',
  HOMEPATH: '\\Users\\oy',
  JAVA_HOME: 'D:\\JavaDevelop\\jdk1.8.0_111',
  LOCALAPPDATA: 'C:\\Users\\oy\\AppData\\Local',
  LOGONSERVER: '\\\\LAPTOP-SHVUJ9TS',
  MYSQL_HOME: 'D:\\JavaDevelop\\mysql-5.1.69-winx64',
  NUMBER_OF_PROCESSORS: '8',
  OneDrive: 'C:\\Users\\oy\\OneDrive',
  OneDriveConsumer: 'C:\\Users\\oy\\OneDrive',
  OS: 'Windows_NT',
  Path:
   'C:\\Users\\oy\\AppData\\Roaming\\npm;D:\\DevTools\\php-5.6.40-Win32-VC11-x64;D:\\JavaDevelop\\mysql-5.1.69-winx64\\bin;D:\\JavaDevelop\\jdk1.8.0_111\\bin;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;D:\\DevTools\\nodejs\\;C:\\Users\\oy\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\oy\\AppData\\Roaming\\npm;',
  PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL',
  PHP_HOME: 'D:\\DevTools\\php-5.6.40-Win32-VC11-x64',
  PROCESSOR_ARCHITECTURE: 'AMD64',
  PROCESSOR_IDENTIFIER: 'AMD64 Family 23 Model 17 Stepping 0, AuthenticAMD',
  PROCESSOR_LEVEL: '23',
  PROCESSOR_REVISION: '1100',
  ProgramData: 'C:\\ProgramData',
  ProgramFiles: 'C:\\Program Files',
  'ProgramFiles(x86)': 'C:\\Program Files (x86)',
  ProgramW6432: 'C:\\Program Files',
  PSModulePath:
   'C:\\Users\\oy\\Documents\\WindowsPowerShell\\Modules;C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules',
  PUBLIC: 'C:\\Users\\Public',
  SESSIONNAME: 'Console',
  SystemDrive: 'C:',
  SystemRoot: 'C:\\WINDOWS',
  TEMP: 'C:\\Users\\oy\\AppData\\Local\\Temp',
  TMP: 'C:\\Users\\oy\\AppData\\Local\\Temp',
  USERDOMAIN: 'LAPTOP-SHVUJ9TS',
  USERDOMAIN_ROAMINGPROFILE: 'LAPTOP-SHVUJ9TS',
  USERNAME: 'oy',
  USERPROFILE: 'C:\\Users\\oy',
  windir: 'C:\\WINDOWS' }

 

五、setImmediate

// 下個隊列
setImmediate(() => {
    console.log('setImmediate');
});

setTimeout(() => {
    console.log('setTimeout');
});
// 當前隊列的隊尾
process.nextTick(() => {
    console.log('nextTick');
});

// 結果:
nextTick
setTimeout
setImmediate
相關文章
相關標籤/搜索