I’m starting to play around with Node.js and specifically with the LessCSS compiler. I saw this call on the source code of the lessc script:
new(less.Parser) ({
paths: [path.dirname(input)].concat(options.paths),
optimization: options.optimization,
filename: input,
strictImports: options.strictImports,
dumpLineNumbers: options.dumpLineNumbers
}).parse(data, function (err, tree) {
//...
});
What I don’t understand is how that new(lessParser)({}) call works.
From what I’ve been reading about Object-oriented Javascript I only found examples of new being called like so: object = new Someting
So the questions are: how does new work in that case? Is it calling less.Parser as the constructor? Also, what are the parenthesis (following the new call) executing/returning?
Thanks!
The code you posted is
exactlytechnically (or more or less) the same as doingBut with a few shortcuts and with some parenthesis in “unconventional” places.
JavaScript is a loose type language and it is interpreted in tokens. Which means that it is not too limiting on it’s syntax, as long as the grammar is respected.
This let things like :
be completely valid, and will create a new instance of
bas it is the first one non-false value defined after the tested variablea. (Note that it does not check ifbis actually afunction. Ifbwas non-false and not a function, the JS engine would throw that it is not a function at run-time.)