JavaScript的Async和Sync——一個簡單的code

JavaScript 是一種解釋性語言。node

Python也是一種解釋性語言。async

對於解釋性語言,須要瞭解該語言的Integrated Development Environment,好比命令行。ide

Python中的sync,ui

  for x in [1,2,3,4,5,6,7,8,9,0]命令行

    y = 0ip

    while y < 1000000:ci

      y += 1it

    print xio

JavaScript中的Async:console

  fs = require('fs');

  fs.writeFile('./write.txt', 'Hello World!', function(err){

    if (err) throw err;

    console.log('Oh, it writes successfully!');

    console.log("I'm async and I will show later");

  }

  console.log(" HAHA,In fact, I'm sync and  I will show earlier");

分別用Python和node來運行上面兩段代碼,能夠感覺下區別。

我的以爲,回調回調,回過頭來再調用。

有句話,「大家給我等着,我還會回來的」,我的覺得是差很少的Async。

 

// // *** An anonymous function used as closure ***

var baz;

(function() {  

  var foo = 10, bar = 2;  

  baz = function() {

    return foo*bar;  

  };

})();

 // // Javascript has ***function level*** scope.

// // * This means a variable defined within a function is ***NOT*** accessible ***outside*** of it.

try {  

  console.log(foo);

}

catch(err) {

  console.log(err); //error

}

try {

  console.log(bar);

}

catch(err) {

  console.log(err);  //error

}

// // Javascript has ***lexcially level*** scope, also called ***Static Scope***

// // * This means functions run in the scope they are ***defined in***, ***NOT*** the scope they are ***excuted in***.

console.log(baz());

相關文章
相關標籤/搜索