JavaScript的Async和Sync——一个简单的code

JavaScript 是一种解释性语言。

Python也是一种解释性语言。

对于解释性语言,需要了解该语言的Integrated Development Environment,比如命令行。

Python中的sync,

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

    y = 0

    while y < 1000000:

      y += 1

    print x

JavaScript中的Async:

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

你可能感兴趣的:(JavaScript)