New Features of JavaScript1.8 in W3C Group

Source: Internet
Author: User

JavaScript 1.8 is intended to be used as part of Gecko 1.9 (to be merged in Firefox 3. Compared with JavaScript 1.7, this is only a small update, but it does contain traces of evolution to ECMAScript 4/JavaScript 2. JavaScript 1.8 will also include all the new features in JavaScript 1.6 and JavaScript 1.7.
Use JavaScript 1.8
To use the new features of JavaScript 1.8 in HTML, you need to write as follows:
<Script type = "application/javascript; version = 1.8">... your code... </script>
When using JavaScript shell, JavaScript XPCOM components, or XUL <script> elements, the latest JS version (JS1.8 in Mozilla 1.9) is automatically used ).
If you need to use the new keywords "yield" and "let", you must specify the version 1.7 or later, because the code that has been written may use these two keywords as the variable name or function name. If no new keywords are used, you do not need to specify the JavaScript version.
Expression Closure
This newly added feature is actually a convenient way to write simple functions, making this language more similar to the typical Lambda notation.
JavaScript 1.7 and earlier versions:
Function (x) {return x * x ;}
Java 1.8:
Function (x) x * x
This syntax allows you to omit curly braces and 'Return 'statements-to do their work implicitly. Writing in this way only looks a little shorter on the surface, and there is no other benefit.
Example:
Simple syntax for binding event processors:
Document. addEventListener ("click", function () false, true );
Use this definition with a write Array Function in JavaScript 1.6:
Elems. some (function (elem) elem. type = "text ");
Generator expression
This newly added feature allows you to easily create a generator (introduced in JavaScript 1.7 ). You usually need to create a custom function that contains a yield, but this new feature allows you to create an independent generator handle using syntax similar to the array concept.
In JavaScript 1.7, you may need to write the following code to create a custom Generator for an object:
Function add3 (obj ){
For (let I in obj)
Yield I + 3;
}
Let it = add3 (someObj );
Try {
While (true ){
Document. write (it. next () + "<br> \ n ");
}
} Catch (err if err instanceof StopIteration ){
Document. write ("End of record. <br> \ n ");
}
In JavaScript 1.8, you can avoid the trouble of recreating the generator function, instead of using a generator expression:
Let it = (I + 3 for (I in someObj ));
Try {
While (true ){
Document. write (it. next () + "<br> \ n ");
}
} Catch (err if err instanceof StopIteration ){
Document. write ("End of record. <br> \ n ");
}
The generator expression can also be passed to a function as a numeric value. It is worth noting that the generator is run only when it is absolutely necessary and useful (the structure of the array is not prefixed as the condition of the typical array concept ). The difference can be seen in the following example:
Use JavaScript 1.7 array concepts
HandleResults ([I for (I in obj) if (I> 3)]);
Function handleResults (results ){
For (let I in results)
//...
}
Use the generator expression of JavaScript 1.8
HandleResults (I for (I in obj) if (I> 3 ));
Function handleResults (results ){
For (let I in results)
//...
}
The biggest difference between the two examples is that when using the generator expression, you only need to loop the 'obj 'structure once, total; in the first example, it will be recycled once in recursion.
JavaScript 1.8.1
JavaScript 1.8.1 is included in Gecko1.9.1 (will be integrated into Firefox3.5. This version has only a few updates and is mainly used to add real-time compilation tracking. For details, see Tracemonkey just-in-time compiler.
Of course, a significant change is that the callback branch in the API is removed and the callback operation is replaced. For details, see detailed in this newsgroup posting.
Added
Object. getPrototypeOf ()
This new method returns the prototype of a specified object.
This method returns the prototype of the specified object.
New trim methods on the String object
The String object now has trim (), trimLeft (), and trimRight () methods.
The String object now has the trim (), trimLeft (), and trimRight () methods.

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.