I was browsing through the jade templating engine source code and I am trying to figure out what this statement means.
I get that it’s going to try and instantiate options.compiler and if that fails instantiate Compiler, but the next part confuses me… Is this saying to call parser.parse and declare the returned value as a variable? If so, why is the rightmost paren around options?
var compiler = new (options.compiler || Compiler)(parser.parse(), options)
, js = compiler.compile();
.
Here’s some more context if that helps
function parse(str, options){
try {
// Parse
var parser = new Parser(str, options.filename, options);
// Compile
var compiler = new (options.compiler || Compiler)(parser.parse(), options)
, js = compiler.compile();
Let’s break it down.
This expression seems to be designed to look for a “class” (well, technically a constructor function, this being JavaScript). The options object may be used to specify it, or else it will fall back to whatever is referenced by
Compiler.Okay, now this makes more sense. We’re invoking a constructor. It’s just that the “class” was chosen dynamically.
When we income the constructor, we’re passing in two parameters. The first is the result of calling the
parsemethod of theparserobject, and the second is the options object from earlier.That unholy mess is stored in the
compilervariable.You can declare and assign multiple variables in the same
varstatement, so that confuses things further. But the last part is pretty easy to understand by itself.That statement could, and probably should, be broken down into multiple lines… but it is what it is.