Why do we wrap our variables in curly braces, like {EventEmitter} = require 'events'
, when extending a Node.js class?
For example, Trevor Burnham, in his tutorial on Event-Driven CoffeeScript, extends Node's EventEmitter this way:
{EventEmitter} = require 'events' class Rooster extends EventEmitter constructor: -> @on 'wake', -> console.log 'COCKADOODLEDOO!' (foghorn = new Rooster).emit 'wake' # COCKADOODLEDOO!
By default, each class in Node. js can extend only a single class. That means, to inherit from multiple classes, you'd need to create a hierarchy of classes that extend each other. If you're with NPM v4 or lower, just append a -S to the install command to automatically add it to the dependencies in package.
In javascript code, curly brackets are used to deconstruct an object.
It's an object literal. Show activity on this post. Basically the curly braces {} are the another way for creating objects in javascript. This is equivalent to the "new Object()" syntax.
Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.
This:
{EventEmitter} = require 'events'
is equivalent to this JavaScript:
var EventEmitter; EventEmitter = require('events').EventEmitter;
When you require 'events'
, you're getting an object back with the module's exports, one of those exports is the EventEmitter
"class". Using {EventEmitter}
is just an idiomatic shortcut for pulling EventEmitter
out of the object that require 'events'
returns; you could also say this:
EventEmitter = require('events').EventEmitter
if you prefer. The braced version starts to come in handy when you want to extract more than one part of an object; for example, this:
{a, b} = c
is like this JavaScript:
var a, b; a = c.a; b = c.b;
The Destructuring Assignment section of the CoffeeScript documentation might make some good reading right about now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With