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());

相关文章
相关标签/搜索