Posts

Showing posts from November, 2013

Initializing an array with values

It is not a frequent use case, but it comes up occasionally: Producing an array [1] of a given length that is filled with values. This blog post explains how to do it and what to watch out for. Let us start with something simple: producing an array of length n whose first element is 0, second element is 1, etc. Array.prototype.map() Once you have an array with length n , you can use the array method map() to fill it appropriately. For example, to produce the array [0, 1, 2] , any array of length 3 will do: > var arr = [null, null, null]; > arr.map(function (x, i) { return i }) [ 0, 1, 2 ] Alas, map() skips holes, but preserves them [2] , which is why an empty array (with only holes) does not work for us: > new Array(3).map(function (x, i) { return i }) [ , , ] The next two sections look at ways of creating an array with a given length. Filling an array via apply() Function.prototype.apply() treats holes as if they were undefined elements [3] . Theref...

ECMAScript 6 modules in future browsers

Update 2013-11-22: David Herman has published the slide deck “ Status Report: ES6 Modules ”. [1] is an introduction to ECMAScript 6 modules and how they can be used in current browsers. In contrast, this blog post explains how future browsers will support them natively. As part of that support, we will get the <module> tag, a better version of the <script> tag. Browsers: asynchronous modules, synchronous scripts Modules. To be in line with JavaScript’s usual run-to-completion semantics, the body of a module must be executed without interruption. That leaves two options for importing modules: Load modules synchronously, while the body is executed. That is what Node.js does. Load all modules asynchronously, before the body is executed. That is how AMD modules are handled. It is the only option for browsers, because modules are loaded over the internet. Otherwise, execution would pause for too long. As an added benefit, this approach allows one to load multipl...

Immediately invoked constructors and object literals

By now, you are probably familiar with immediately invoked function expressions (IIFEs, [1] ). This blog post looks at immediately invoked constructors and immediately invoked object literals. Immediately invoked constructors Andrea Giammarchi (@WebReflection) recently reminded us that you can immediately invoke constructors. That is useful for setting up plain objects. Let’s look at a (slightly contrived) example: var obj = new function () { // open IIC var uid = computeUID(); this.first = 'Jane'; this.last = 'Doe'; this.uid = uid; }(); // close IIC The immediately invoked constructor provides you with a private scope, in which the variable uid lives. You can then use this to set up the instance that has been created by new . Additionally, new returns that instance for you. You can save a few characters by omitting the parentheses at the end: var obj = new function () { // open IIC ... }; // close IIC The ...