Let apply and new collaborate simultaneously

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.