å
¨å±å¯¹è±¡ä¸å
¨å±åé
global ææ ¹æ¬çä½ç¨æ¯ä½ä¸ºå
¨å±åéç宿主ãæç
§ECMAScript çå®ä¹ï¼æ»¡è¶³ä»¥ä¸æ¡ 件çåéæ¯å
¨å±åéï¼
å¨æå¤å±å®ä¹çåéï¼
å
¨å±å¯¹è±¡çå±æ§ï¼
éå¼å®ä¹çåéï¼æªå®ä¹ç´æ¥èµå¼çåéï¼ã
å½ä½ å®ä¹ä¸ä¸ªå
¨å±åéæ¶ï¼è¿ä¸ªåéåæ¶ä¹ä¼æ为å
¨å±å¯¹è±¡çå±æ§ï¼åä¹äº¦ç¶ãéè¦æ³¨ æçæ¯ï¼å¨Node.js ä¸ä½ ä¸å¯è½å¨æå¤å±å®ä¹åéï¼å 为ææç¨æ·ä»£ç é½æ¯å±äºå½å模åçï¼ è模åæ¬èº«ä¸æ¯æå¤å±ä¸ä¸æã
注æï¼ æ°¸è¿ä½¿ç¨var å®ä¹åé以é¿å
å¼å
¥å
¨å±åéï¼å 为å
¨å±åéä¼æ±¡æ å½å空é´ï¼æé«ä»£ç çè¦åé£é©ã
process
process æ¯ä¸ä¸ªå
¨å±åéï¼å³ global 对象çå±æ§ã
å®ç¨äºæè¿°å½åNode.js è¿ç¨ç¶æ ç对象ï¼æä¾äºä¸ä¸ªä¸æä½ç³»ç»çç®åæ¥å£ãé常å¨ä½ åæ¬å°å½ä»¤è¡ç¨åºçæ¶åï¼å°ä¸äºè¦ åå®æ交éãä¸é¢å°ä¼ä»ç»process 对象çä¸äºæ常ç¨çæåæ¹æ³ã
process.argvæ¯å½ä»¤è¡åæ°æ°ç»ï¼ç¬¬ä¸ä¸ªå
ç´ æ¯ nodeï¼ç¬¬äºä¸ªå
ç´ æ¯èæ¬æ件åï¼ ä»ç¬¬ä¸ä¸ªå
ç´ å¼å§æ¯ä¸ªå
ç´ æ¯ä¸ä¸ªè¿è¡åæ°ã
console.log(process.argv);
å°ä»¥ä¸ä»£ç åå¨ä¸ºargv.jsï¼éè¿ä»¥ä¸å½ä»¤è¿è¡ï¼
$ node argv.js 1991 name=byvoid --v "Carbo Kuo"
[ 'node',
'/home/byvoid/argv.js',
'1991',
'name=byvoid',
'--v',
'Carbo Kuo' ]
process.stdoutæ¯æ åè¾åºæµï¼é常æ们使ç¨ç console.log() åæ åè¾åºæå° å符ï¼è process.stdout.write() å½æ°æä¾äºæ´åºå±çæ¥å£ã
process.stdinæ¯æ åè¾å
¥æµï¼åå§æ¶å®æ¯è¢«æåçï¼è¦æ³ä»æ åè¾å
¥è¯»åæ°æ®ï¼ ä½ å¿
é¡»æ¢å¤æµï¼å¹¶æå¨ç¼åæµçäºä»¶ååºå½æ°ã
process.stdin.resume();
process.stdin.on('data', function(data) {
process.stdout.write('read from console: ' + data.toString());
});
process.nextTick(callback)çåè½æ¯ä¸ºäºä»¶å¾ªç¯è®¾ç½®ä¸é¡¹ä»»å¡ï¼Node.js ä¼å¨ ä¸æ¬¡äºä»¶å¾ªç¯è°ååºæ¶è°ç¨ callbackã
åå¦è
å¾å¯è½ä¸ç解è¿ä¸ªå½æ°çä½ç¨ï¼æä»ä¹ä»»å¡ä¸è½å¨å½ä¸æ§è¡å®ï¼éè¦äº¤ç»ä¸æ¬¡äº 件循ç¯ååºæ¥åå¢ï¼
æ们讨论è¿ï¼Node.js éåI/O å¯éåçåºç¨ï¼èä¸æ¯è®¡ç®å¯éåçåºç¨ï¼ å 为ä¸ä¸ªNode.js è¿ç¨åªæä¸ä¸ªçº¿ç¨ï¼å æ¤å¨ä»»ä½æ¶å»é½åªæä¸ä¸ªäºä»¶å¨æ§è¡ã
å¦æè¿ä¸ªäº 件å ç¨å¤§éçCPU æ¶é´ï¼æ§è¡äºä»¶å¾ªç¯ä¸çä¸ä¸ä¸ªäºä»¶å°±éè¦çå¾
å¾ä¹
ï¼å æ¤Node.js çä¸ ä¸ªç¼ç¨ååå°±æ¯å°½é缩çæ¯ä¸ªäºä»¶çæ§è¡æ¶é´ãprocess.nextTick() æä¾äºä¸ä¸ªè¿æ ·ç å·¥å
·ï¼å¯ä»¥æå¤æçå·¥ä½ææ£ï¼åæä¸ä¸ªä¸ªè¾å°çäºä»¶ã
functiondoSomething(args, callback) {
somethingComplicated(args);
callback();
}
doSomething(functiononEnd() {
compute();
});
æ们å设compute() åsomethingComplicated() æ¯ä¸¤ä¸ªè¾ä¸ºèæ¶çå½æ°ï¼ä»¥ä¸ çç¨åºå¨è°ç¨
doSomething() æ¶ä¼å
æ§è¡somethingComplicated()ï¼ç¶åç«å³è°ç¨ åè°å½æ°ï¼å¨ onEnd() ä¸åä¼æ§è¡
compute()ãä¸é¢ç¨process.nextTick() æ¹åä¸ é¢çç¨åºï¼
functiondoSomething(args, callback) {
somethingComplicated(args);
process.nextTick(callback);
}
doSomething(functiononEnd() {
compute();
});
æ¹ååçç¨åºä¼æä¸é¢èæ¶çæä½æå为两个äºä»¶ï¼åå°æ¯ä¸ªäºä»¶çæ§è¡æ¶é´ï¼æé«äº 件ååºé度ã
注æï¼ ä¸è¦ä½¿ç¨setTimeout(fn,0)代æ¿process.nextTick(callback)ï¼ åè
æ¯åè
æçè¦ä½å¾å¤ã
æ们æ¢è®¨äºprocess对象常ç¨çå 个æåï¼é¤æ¤ä¹å¤processè¿å±ç¤ºäºprocess.platformã process.pidãprocess.execPathãprocess.memoryUsage() çæ¹æ³ï¼ä»¥åPOSIX è¿ç¨ä¿¡å·ååºæºå¶ã
console
console ç¨äºæä¾æ§å¶å°æ åè¾åºï¼å®æ¯ç±Internet Explorer çJScript å¼ææä¾çè°è¯ å·¥å
·ï¼åæ¥éæ¸æ为æµè§å¨çäºå®æ åã
Node.js 沿ç¨äºè¿ä¸ªæ åï¼æä¾ä¸ä¹ æ¯è¡ä¸ºä¸è´ç console 对象ï¼ç¨äºåæ åè¾åºæµï¼stdoutï¼ææ åé误æµï¼stderrï¼è¾åºå符ã console.log()ï¼åæ åè¾åºæµæå°å符并以æ¢è¡ç¬¦ç»æã
console.log æ¥åè¥å¹² 个åæ°ï¼å¦æåªæä¸ä¸ªåæ°ï¼åè¾åºè¿ä¸ªåæ°çå符串形å¼ãå¦ææå¤ä¸ªåæ°ï¼å 以类似äºC è¯è¨ printf() å½ä»¤çæ ¼å¼è¾åºã
第ä¸ä¸ªåæ°æ¯ä¸ä¸ªå符串ï¼å¦æ没æ åæ°ï¼åªæå°ä¸ä¸ªæ¢è¡ã
console.log('Hello world');
console.log('byvoid%diovyb');
console.log('byvoid%diovyb', 1991);
è¿è¡ç»æ为ï¼
Hello world
byvoid%diovyb
byvoid1991iovyb
console.error()ï¼ä¸console.log() ç¨æ³ç¸åï¼åªæ¯åæ åé误æµè¾åºã
console.trace()ï¼åæ åé误æµè¾åºå½åçè°ç¨æ ã
console.trace();
è¿è¡ç»æ为ï¼
Trace:
at Object.<anonymous> (/home/byvoid/consoletrace.js:1:71)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)
温馨提示:答案为网友推荐,仅供参考