javascriptnew-operator

Why wrap the constructor passed to the new operator in parentheses?


I found next form of new operator in the less documentation.

var parser = new (less.Parser) ({
    pathes [
        '.',
        './css'
    ],
    filename: 'style.less'
});

What the differences from the usual form?

var parser = new less.Parser({
    patches [
        '.',
        './css'
    ],
    filename: 'style.less'
});

Can it be combined with .apply method?


Solution

  • There's no difference between

    var parser = new (less.Parser) (...);
    

    and

    var parser = new less.Parser (...);
    

    The parens around less.Parser are entirely superfluous.

    Can it be combined with .apply method?

    It's difficult to use a function designed to be called via new (a "constructor function") via apply, because new does a lot of things. (This is true of both forms of the above, since — again — they're the same thing.) I talk about using apply with constructor functions more in this other answer here on Stack Overflow.