Introduction to the new features of the JavaScript1.8 of the Consortium group-Basic Knowledge

Source: Internet
Author: User
Tags generator numeric value
The JavaScript 1.8 plan is used as part of Gecko 1.9, which will be merged in Firefox 3. This is a small update compared to JavaScript 1.7来, but it does contain traces of evolution to ECMAScript 4/javascript 2. JavaScript 1.8 will also contain all the new features in JavaScript 1.6 and JavaScript 1.7.
using JavaScript 1.8
To use the new features of JavaScript 1.8 in HTML, you need to write the following:
<script type= "application/javascript;version=1.8" > ... Your code ... </script>
When you use JavaScript shells, JavaScript XPCOM components, or XUL <script> elements, you automatically use the latest JS version (JS1.8 in Mozilla 1.9).
If you need to use the New keyword "yield" and "let", you should specify the version 1.7 or higher, because the code that has been written might use these two keywords as variable names or function names. If you do not use any new keywords, you do not need to specify the version of JavaScript.
An expression closure
This newly added feature is actually a handy way of writing simple functions, making the language more like a typical lambda notation.
JavaScript 1.7 and older versions:
function (x) {return x * x;}
JavaScript 1.8:
function (x) x * x
This syntax allows you to omit curly braces and ' return ' statements--to do their work implicitly. Written in this way, it just looks shorter on the surface and has no other benefits.
Example:
a simple notation for binding event handlers:
Document.addeventlistener ("click", Function () false, true);
Use this definition in conjunction with a write array function in JavaScript 1.6:
Elems.some (function (elem) Elem.type = = "text");
Builder Expression
This newly added feature allows you to simply create a generator (introduced in JavaScript 1.7). Usually you need to create a custom function that contains a yield, but this new feature allows you to create a separate generator handle using syntax similar to the array concept.
In JavaScript 1.7, you might want to write code like the following 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 circumvent the trouble of rebuilding a generator function and use a generator expression instead:
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 important to note that the generator is run when it is absolutely necessary to be useful (unlike the typical array concept of the conditions, the structure of the array beforehand). This distinction can be seen in the following example:
using the JavaScript 1.7 array idea
Handleresults ([I for (i in obj) if (i > 3)]);
function Handleresults (results) {
For (Let I in results)
// ...
}
Builder expressions that use JavaScript 1.8
Handleresults (I for (i in obj) if (i > 3));
function Handleresults (results) {
For (Let I in results)
// ...
}
The biggest difference between these two examples is that when you use a builder expression, you only need to loop that ' obj ' structure once, in total, and in the first example, you'll cycle through recursion again.
JavaScript 1.8.1
The JavaScript 1.8.1 is contained in Gecko1.9.1 (which will be consolidated into Firefox3.5). This version has only a few updates, mainly focused on adding just-in-time compile tracking, see: Tracemonkey just-in-time compiler.
Of course, the obvious change is to remove the callback branch in the API, replacing the callback operation, see: Detailed in this newsgroup posting.
Part of the Add
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 () method.

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.