Javascript1.7 new syntax

Source: Internet
Author: User

 

Web technology has developed rapidly over the past two years. As the development environment changes, we can also use new technologies to increase productivity. Javascript1.6 adds a series of new methods to the array, which may be familiar to everyone.

Next, let's take a look at the new changes brought about by javascript1.7. To use the new version of javascript, we need to use a browser that supports the new version. For example, firefox supports javascript1.8.5 at the maximum. At the same time, we need to handle the mime type:

<Script type = "application/javascript; version = 1.7">

 

For example:

1 <script type = "application/javascript; version = 1.7">

2 void function (){

3 if (true ){

4 var a = 1;

5 console. log (a); // 1

6}

7 console. log (a); // 1

8 }()

9 </script>

 

 

1. yield keywords

 

Yield is similar to return, and returns a result. However, return will jump out of the function, and yield can be used multiple times until the operation cannot be performed. (Yield is also a c # keyword ).

 

2. Generators

 

When a function uses yield to specify the return value, it can be used as a generator. If the yield function is called, an instance of the generator is obtained. Each time the next method of the generator instance is called, an operation result is obtained. If the boundary of the generator is exceeded, an object StopIteration error is returned. Because the yield relationship cannot accurately obtain the generator length, use the try statement to process it. When the close method is called after the generator is used, the generator is forcibly disabled.

 

1 function getNumber (){

2 for (let I = 0; I <10; I ++ ){

3 yield I * 2;

4}

5}

6 var generators = getNumber ();

7 try {

8 while (generators ){

9 console. log (generators. next ());

10}

11} catch (e ){

12

13} finally {

14 generators. close ();

15}

 

 

3. iterator

 

If you hate using for and for in, try the iterator. The same goal can be achieved even without loops, and the iterator can support any type of objects.

 

1 var arr = ['A', 'B', 'C'], it = new Iterator (arr); // {'1': 'A ', '2': 'B', '3': 'C '}

2 try {

3 while (it ){

4 console. log (it. next (); // [0, "a"] [1, "B"] [2, "c"] // [1, "a"] [2, "B"] [3, "c"]

5}

6} catch (e ){};

 

 

The next method of the iterator will return an array. The first item is the subscript or key, and the second item is the corresponding value. If the boundary is exceeded, an object StopIteration error is returned.

4. array comprehension.

This is the syntax from python. Introducing JavaScript can greatly improve productivity.

Syntax: array = [value for each (variable in values) condition];

Var a = [222,];

A.

1 for each (I in a) {if (I) {console. log (I) }}// similar to the old usage of for in.

B.

1 var B = [I * 2 for each (I in a) if (I % 3 = 0)]; // [6, 12, 0,444] Get a new array

 

C.

1 var B = (I * 2 for each (I in a) if (I % 3 = 0); // obtain a generator.

2

3

4 try {

5

6 while (B ){

7

8 console. log (B. next (); // 6 12 0 444

9

10}

11

12

13} catch (e ){};

 

D. processing object

 

1 for each (let [j, k] in new Iterator ()){

2

3

4 console. log (j + ''+ k)

5

6

7 };

 

5. Structure assignment

This is very simple

 

1 var [a, B] = [1, 2, 3, 4]; // a = 1 B = 2

2 var {width: px, height: pec} = {width: 80, height: '000000'}; // px 80 pec 50%

 

 

6. let keywords create block-level scope

Currently, the wide-used javascript1.5 has no block-level scope. I used var to define variables. Because of the rising relationship of parser variables, I often step on the mines accidentally. Now, the gospel is coming!

1 void function (){

2 if (true ){

3 let a = 1;

4 console. log (a); // 1

5}

6 console. log (a); // undefined

7 }()

 

Look at var

1 void function (){

2 if (true ){

3 var a = 1;

4 console. log (a); // 1

5}

6 console. log (a); // 1

7 }()

 

 

 

From Phoenix

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.