JavaScript new features Let,function*,yield,promise

Source: Internet
Author: User

Since went to Baidu for a long time did not update the blog, read the previous written east, before the small company or relatively idle AH. The irony is this time to change jobs to small companies, just remember to write a blog ...

After going to Baidu to learn Python, and then the front of GG gone, I do not want to use Python, the full effort to switch to Nodejs.

More and more like the JS family, recently looked at the new features of ECMA6, and indeed enhanced the vitality of JS AH. See foreign Asmjs Daniel's video said, JS author only 10 days to create the language.

So hopefully the ECMA will continue to improve.

A little more nonsense. Here's the dry goods. Note that the code will be executed with node--harmony, and the code will run in strict mode.

Let, in fact, is the block-level scope declaration variable var. The var keyword of the previous JS is a non-block-level scope, but a function-level.

For example arr=[0,1,2], we often write loops for (var i=0,len=arr.length; i < Len; i++) {}, in fact, after the loop this I is also accessible to 2. This makes it easy to pollute environment variables.

If Let,for (let i=0,len=arr.length; i<len; i++) {} is used, then the loop i is undefined.


function*, declare the constructor, return {Value:v,done:true_or_false}. The constructor can call next to get the next value, which can be constructed like a random number generator.


Yield and function* are used together. In the constructor function, yield can pause and then return the value of the current expression.

For example function A () {yield 1;yield 2;}; var gen=a (); Console.log (Gen.next ()); Console.log (Gen.next ()); Console.log (Gen.next ()); The result is {value:1,done:false},{ Value:2,done:false},{value:undefined,done:true}. After the first line of yield 1 that constructs a function is executed, the a function exits and saves the context, and when you execute next again the A function context resumes execution of the next line B.


Promise, this non-ECMA6 feature, Html5rocks has a very good article written. Mainly to solve the problem of multi-layer nesting. Converts a nested callback into a chained call.

JavaScript new features Let,function*,yield,promise

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.