globalnode
var聲明不能直接聲明在global上閉包
global.a = 100 ;
console.log( global.a ) //輸出必須帶global
console.log(global)
/*
* 五個形參 咱們也叫它全局屬性 在任何文件中均可以使用
* (function(export , require , module , __dirname , __filename ){
* console.log(this) //閉包致使了this的變化
* 2.global.js
* })()
*
* */
process 進程異步
次開啓一個程node序,就會開一個進程,代碼執行完畢就自動關閉進程
process.nextTick() //下一隊列 (當前隊列的底部),同步代碼執行完後會執行的方法
process.pid() //進程id號
process.kill() // 殺掉進程
process.cwd() // dirname 不能夠更改的 當前工做目錄current working directory
process.exit() //退出進程
// 3秒後關閉進程
setInterval(function () {
process.exit( )
},3000)
異步的三個方法
setTimeout() 惟一能設置時間的
setInterval()
setImmediate() netxTick後執行的方法
//time 和 timeEnd 的參數必須傳入一個字符串 ,而且是同一個
console.time('start') //計時
for(var i=0; i<1000 ; i++){ }
console.timeEnd('start')
function eat(who,food) {
console.log(this) //settimeout 中的this指的是本身
console.log(who+"..."+food)
}
//在第二個參數以後都是給運行的函數傳遞值
//setTimeout(eat,1000,'小花',"冰激凌")
//bind能夠改變this指向
setTimeout(eat.bind(global),1000,'小花',"冰激凌")
//執行快慢: 同步 -- process.nextTick -- setImmediate -- setTimeout
例子:
console.log("a");
process.nextTick(function () {
console.log("去廁所。。。")
})
console.log("b")
setImmediate(function () { //當即執行
console.log("掃地")
})
setTimeout(function () {
console.log("端飯" )
},2000) //若是不寫事件約等於 setImmediate時間 可能會在setImmediate以前執行
console
console.log()
console.error()
console.info()
console.warn()
console.time()
console.timeEnd()
__dirname __filename