coffeescript - Curly Braces when Extending Node.js Class -
why wrap our variables in curly braces, {eventemitter} = require 'events'
, when extending node.js class?
for example, trevor burnham, in tutorial on event-driven coffeescript, extends node's eventemitter way:
{eventemitter} = require 'events' class rooster extends eventemitter constructor: -> @on 'wake', -> console.log 'cockadoodledoo!' (foghorn = new rooster).emit 'wake' # cockadoodledoo!
this:
{eventemitter} = require 'events'
is equivalent javascript:
var eventemitter; eventemitter = require('events').eventemitter;
when require 'events'
, you're getting object module's exports, 1 of exports eventemitter
"class". using {eventemitter}
idiomatic shortcut pulling eventemitter
out of object require 'events'
returns; this:
eventemitter = require('events').eventemitter
if prefer. braced version starts come in handy when want extract more 1 part of object; example, this:
{a, b} = c
is javascript:
var a, b; = c.a; b = c.b;
the destructuring assignment section of coffeescript documentation might make reading right now.
Comments
Post a Comment