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)