DailyJS

DailyJS

The JavaScript blog.


Tagharmony
Featured

modules harmony amd

An Alternative to AMD: CMD

Posted on .

By now most of us use AMD or CommonJS as our module API. With Harmony on the horizon, there doesn't seem to be much need to innovate where libraries like RequireJS have established a niche. Despite this, John Wu sent me his module loader that can be found in his project wd.js.

This approach uses a chainable API which allows modules to be loaded asynchronously in a browser-friendly way, without AMD's less fluent syntax:

wd.module('core.util')  
  .require('core.bar')
  .require('core.foo')
  .body(function() {
    // foo and bar are now loaded
  });

John calls this style CMD, which stands for Chainable Module Definition. Although it seems like an obvious idea, it's new to me and I think it fits in well with other modern JavaScript libraries.

Featured

books node harmony ES6

Read Only References, Getting MEAN

Posted on .

Creating Read-Only References

I was working on an API where the desktop developers were worried I could break their code if my server had a typo in one of the API responses. The server was returning JSON objects, and some of the properties were important, so if they were spelled incorrectly it could be dangerous.

I wrote unit tests to ensure the API responses were the ones they expected, and used Object.defineProperty to make the API response properties read only. But what if you wanted some objects to have read/write access to an object, and others to be set as read only?

Sergey Bolshchikov sent in his post about this topic, called Creating Read-Only References. He suggests using Object.observe, so certain objets can subscribe to changes.

Object.observe is basically the future standard way to do databinding. Sergey mentions some polyfills for it, so you can use it in browsers.

Getting MEAN with Mongo, Express, Angular, and Node

Simon Holmes is writing a book about Mongo, Express, Angular, and Node. I've used exactly this cocktail of technologies for some of my commercial work over the last two years, but I'd never thought of the acronym MEAN before. The marketing spin is it's the new LAMP.

You can download the first chapter free, but the book is currently being written. If you buy it early you can contribute to its development by providing the author with feedback. The publisher calls this process MEAP (Manning Early Access Program).

Featured

HTML css routing harmony

JavaScript.next, Front-end Guidelines, Crossroads.js

Posted on .

JavaScript.next

Brendan Eich gave a last-minute talk at JSConf
2011
about the future of JavaScript, spurred on
by a CoffeeScript talk. There's good coverage by Ian Elliot in
JavaScript creator talks about the future
which contains some interesting quotes from Brendan:

I advocated strongly for standardizing prototypal inheritance a la CoffeeScript’s class, super, and @ syntactic sugar.

We've seen all this before and what it did to the community during
ECMAScript 4's planning (which I covered on DailyJS in The History of
JavaScript
). I
don't know if this time anything will change, but there's an undeniably
growing movement that wants to push JavaScript's syntax in new
directions.

I've already seen some harsh commentary from JavaScript developers that
I have a lot of respect for -- but if we learned anything from
ECMAScript it's that progress should come in small steps rather than too
much at once.

I've collected some other resources related to this talk:

And I'm still not sure if I should be writing JavaScript.next, JS.next,
JS.Next, JS/Next, JS-Next, or "Harmony"!

Front-end Guidelines

Front End Development Guidelines
(GitHub: taitems / Front-End-Development-Guidelines)
is a document written by Tait Brown that attempts to collate useful
HTML, CSS and JavaScript best practises that he's learned along the
years. The document is easy to read and contains a lot of things that
I've found myself teaching junior developers and designers before.

Crossroads.js

Crossroads.js (millermedeiros / crossroads.js, MIT
License) by Miller Medeiros is a routing library:

I consider the main target to be single page applications with complex navigation paths and also large websites that shares the same JS files across multiple pages or would benefit from this kind of approach. It is also very useful for server-side and RESTful applications.

The API supports "string rules" and regular expressions:

var route1 = crossroads.addRoute('/news/{id}', function(id){
  console.log(id);
});

var route2 = crossroads.addRoute(/^\/lorem\/([a-z]+)$/, function(id){
  console.log(id);
});

Routes can be removed, parsed, and validated.