Skip to content

Chapter 11: JavaScript Subsets and Extensions

February 26, 2012

This entry is a review of “Chapter 11: JavaScript Subsets and Extensions” from “JavaScript: The Definitive Guide”

Some chapters just blow me away with new information, Chapter 11 was one of those chapters. It covers subsets and extensions to the JavaScript language. The subsets were created mostly for security reasons, and usually attempt to encase only the “good parts” of the language. Google Caja is a popular example of one such subset.

More interesting than the subsets were the extensions. Over the last decade Brian Eich has continually evolved the language and while many of his changes made it into EMCAScript 5.0, there are still some changes that have yet to be standardized (but will most likely get standardized in a future version of JavaScript). While the extensions shouldn’t be used for normal web development, they can be used in FireFox extensions or Rhino specific development. Below are my notes on the ones I found to be the most interesting.

  • const – Declares constants in JavaScript. Works much the same way as var. Consts can be assigned only when declared and future assignments are silently ignored. An error occurs if you try to redeclare a const.
  • let – Brings block scoping to variable declaration. Variables are hoisted to the top of their block instead of the top of their function. Let block statements also exist, which allow you to declare and execute variables for a particular block.
  • Destructuring assignments – Allows multiple variables to be assigned at once. Example:

    <script type=”application/javascript;version=1.8″>
    [a, b, [c]] = [1, 2, [3]];
    console.log(“a=”+a);
    console.log(“b=”+b);
    console.log(“c=”+c);
    let {r:red, g:green, b:blue} = {r:1, g:3, b:2};
    console.log(“red=”+red);
    console.log(“green=”+green);
    console.log(“blue=”+blue);
    </script>

    Note that since these features are non-standard, we have to speicify the JavaScript version, which makes trying them out kind of a pain.
  • for/each loop – Similar to the for-in loop, except it iterates over an object’s property values instead of an object’s property names.
  • Iterators – for-in loops have been updated in JavaScript 1.7 to work with iterable objects. These objects have an __iternator__ property that returns an object with a next() method which throws the StopIteration object when it’s done. The for-in object handles all of this behind the scenes though. There’s also a new Iterator function, which allows you to create Iterators for non-iterable objects.
  • Generators – Any function that uses the yield keyword is a generator function. When invoked, a generator function will return a generator object. A generator object represents the current execution state of its generator function. It has a next() method that resumes execution of the function and allows it to continue running until its next yield statement is encountered. A yield statement acts like a return statement (it returns a value). Return statements are allowed in a generator function, but they cannot return a value, and when encountered via the next() method, they cause the next() method to throw the StopIteration object. Generators are useful as iterators. They have a close() method (for resetting) and a send() method (for passing a value to where the last yield statement left off).
  • Array Comprehensions – Borrowed from Python. Allows a shorthand for initializing the elements of an array with the values from another array or iterable object. Example: var evensquares = [x*x for (x in range(0,10)) if (x % 2 === 0)];
  • Generator Expressions – Similar to Array Comprehensions, except wrapped in {}’s instead of []’s, and return a generator object. This allows for lazy evaluation.
  • Shorthand Functions – Allows single-statement functions to be defined without {}’s.
  • Multiple catch exceptions – Multiple catch statements can be used in conjunction with an if statement. Example:
    try {
    x = 1;
    } catch (e if e === "quit") {
    console.log("quit");
    } catch (e if e instanceof ReferenceError) {
    console.log("reference error");
    } catch (e) {
    console.log("other type of exception");
    }
  • E4X – Adds native XML support to JavaScript. I wasn’t a fan of how the resulting code looked, and luckily it looks like it’s a dying extension.

From → Chapters

Leave a Comment

Leave a comment