javascript apply on constructor, throwing “malformed formal parameter”

后端 未结 4 1228
星月不相逢
星月不相逢 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

    To complete @CMS solution and preserve the prototype chain, you can do this:

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

    As a side note, it will not work with IE 8-.

提交回复
热议问题