(The purpose of my existence!) )
For a function call we sometimes pass an array for apply but apply does not work with new!
How do I pass in an array as a parameter at new?
I've really had this situation! Just a few years ago!
Remember the original or immersed in the study stage of a period of time (then no worries all day into the bar, Q Group, all kinds of enthusiastic people think on the memory. )
During the period there was written a piece of code roughly like this
var function (O1,O2) { returnnew F2 (O2,O2);};
... window[' $ ']=f;$ (' div '). Text (' 666 ');
It was also very convenient to use jquery at that time, and it was out of curiosity that I wanted to write the code like this (no need to spit the groove!). )
After a while, it feels like 2. The reference to JQ has also been improved as follows
var function (O1,O2) { returnnew= F.P = { init:function() {} // ... = F.prototype;
Well, it's similar to JQ, and so does JQ.
But 2 parameters to repeat the Write! If there's more than 3 parameters, then I'm bored.
Believe that those who know can think of solutions.
function () { = object.create (Miku.prototype.init.prototype) return= miku.p= { init () { //... = Miku.prototype;
Without the native create, you can also achieve a basic
Here's es6.
class Miku { constructor (... Miku) { console.log (new// true ) Console.log (Miku.reduce (b) =>a+b); // - = reflect.construct (Miku, [6,6,6]);
Reflect provides a convenient solution he's got a lot of ways. Details MDN
* Bedding る
Let apply and new collaborate simultaneously