New javascript features: let, function *, yield, promise

Source: Internet
Author: User

New javascript features: let, function *, yield, promise

I haven't updated my blog for a long time since I went to Baidu. I read the stuff I wrote before. I used to be idle in small companies. The irony is that this time I want to change my job to a small company, I think of writing blogs...

I learned python first after I went to Baidu, and then GG went away. I didn't want to use python anymore, so I tried my best to switch to nodejs.

I like the JS family more and more. I recently read the new features of ECMA6, which actually enhances the vitality of JS. According to a foreign video from ASMJS Daniel, the author of JS created the language in just 10 days.

Therefore, we hope that ECMA can continue to improve.

More nonsense. The following is a dry item. Note that the Code should be executed in node -- harmony and the code should be run in strict mode.

Let is actually the var of the block-level scope declarative variable. Previously, the var keyword of JavaScript is not block-level scope, but function-level.

For example, if arr = [0, 1, 2], we often write a loop for (var I = 0, len = arr. length; I <len; I ++) {}. In fact, after the loop, this I is still accessible 2. In this way, environment variables are easily contaminated.

If let, for (let I = 0, len = arr. length; I


Function * declares the constructor and returns {value: v, done: true_or_false }. The constructor can call next to obtain the next value and construct a random number generator.


Yield and function * are used together. In the constructor function, yield can pause and 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 yield 1 in the first line of the constructor a is executed, function a exits and stores the context. When next is executed again, the context of function a is resumed and the next row of B is executed.


Promise, this non-ECMA6 feature, HTML5Rocks has written quite well. It is mainly used to solve the problem of multi-layer nesting. Converts nested callbacks into chained calls.

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.