The easiest way to throw in Mocha

It turns out done accepts err as arguments, so it is straightforward to throw an error by


done( new Error("Unexpected error") );

Advertisements
Standard

How do you organize your code?

Surprisingly this question has been asked multiple times during interviews. My first response is, “hmm, doesn’t everyone pretty much do the same thing? you use folders and files”

I would normally say, for Backbone, we have index.html, folders for js, css and images. Inside js, there are a main entry file, and then there are folders for models, views, collections, routes and templates. For express.js, even there is no convention, we tried to have folders for models, views, controllers, and routes.

There must be better answers. Should I mention require.js? Or the export and require in node.js? Or should I mention module pattern ?

Of course, there is a StackOverflow answer for it: http://stackoverflow.com/questions/1344605/how-do-you-organize-your-javascript-code

Standard

Pseudo classical, prototypal and functional

– functional inheritance -> prototypal inheritance –> pseudo classical inheritance –> hacked classical inheritance

Functional inheritance

– Functional can be achieved by any language who supports closure scope and dynamic object augmentation.

– The bad parts are duplicated properties and methods / impossible to achieve dynamic class modification.

– The good parts are: real private variables / callable instances.

Pseudo classical inheritance

– For pseudo classical, semi private variable is achieved by name convention, i.e. _variable_name.

– Comparison among pseudo classical, prototypal and functional

pseudo classical prototypal functional
start out simple no sort of yes
keep simple no yes yes
Method sharing Method sharing Method sharing with a performance penalty or capability security
Dynamic class modification Dynamic class modification Avoid using ‘this’
instanceOf works at all level instanceOf works at all level callable instance / instanceOf doesn’t work at all level / no prototypal masking
Optimized by the engine Avoid using ‘new’ and ‘.prototype’ Avoid using ‘new’ and ‘.prototype’
Standard