New.target (ES6)

Source: Internet
Author: User

Today accidentally saw a lexical new.target!

What's this thing? In my mind, the new heel is a constructor! What the hell is this?

Let F = function f () {    if (!new. Target)      throw new Error (' ... ')    //  };  

Check it out and find out what it does is get the target constructor for the current new

You can guarantee that a function is used as a constructor, not just undefined.

Before, you wanted to make sure that the function was new instead of being called directly, maybe that would do it.

Let F = function f () {    if (!) ( This instanceof A))       throw new Error (' ... ');    // ···  };

But a guy with a B-grid can still write that.

Let F = f.apply (Object.create (F.prototype), [])

In ES6, class has checked for you (whether it's new).

New.target will know which class is currently under output F2

Class F2 extends f{  constructor () {    //' F2 '    console.log (new. target.name);}  }

Very unpopular compatibility is pretty miserable. Chrome Firefox 41

You know, that's fine.

New.target (ES6)

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.