javascript apply on constructor, throwing “malformed formal parameter”

后端 未结 4 1222
星月不相逢
星月不相逢 2021-02-04 00:04

thanks to wonderful responses to this question I understand how to call javascript functions with varargs.

now I\'m looking to use apply with a constructor

4条回答
  •  [愿得一人]
    2021-02-04 00:44

    You could use apply and pass an empty object as the this argument:

    var mid_parser = {};
    Parser.apply(mid_parser, mid_patterns);
    

    But that solution will not take care about the prototype chain.

    You could create a Parser object, using the new operator, but without passing arguments, and then use apply to re-run the constructor function:

    var mid_parser = new Parser();
    Parser.apply(mid_parser, mid_patterns);
    

提交回复
热议问题