Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curly Braces when Extending Node.js Class

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! 
like image 345
Jon Saw Avatar asked Sep 28 '12 22:09

Jon Saw


People also ask

How do I extend a class in node JS?

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.

What is curly braces in node JS?

In javascript code, curly brackets are used to deconstruct an object.

Why we use curly braces in JavaScript?

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.

What is the use of curly braces in react?

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.


1 Answers

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.

like image 123
mu is too short Avatar answered Oct 04 '22 15:10

mu is too short