Nonconstructors
Jordan Harband
ljharb at gmail.com
Tue Apr 25 05:19:41 UTC 2017
Arrow functions are not constructible; just use one of those.
On Mon, Apr 24, 2017 at 2:55 PM, J Decker <d3ck0r at gmail.com> wrote:
> function Entity() {
> if (this instanceof Entity) throw new Error("Please do not call with new");
> }
>
> oh the other side; you can bind this to a simple function call...
>
>
> function f() {
> }
> var fWithThis = f.bind( {} /*Something to use as 'this' */ )
>
> call with fWithThis()
>
> and f's this will be the object you passed to bind.
>
> On Mon, Apr 24, 2017 at 1:42 PM, Raul-Sebastian Mihăilă <
> raul.mihaila at gmail.com> wrote:
>
>> I have a dilemma. I like how typically the built-in methods are not
>> constructors (like Array.prototype.forEach). I have cases in which I'm
>> creating a function in which I want to use `this` but I would like the
>> function to not be constructible. There are ways of doing this:
>> - checking new.target - but you have to check and the last time I checked
>> uglifyjs was failing because of it.
>> - ({function() { ... }}).function - but this requires creating an object
>> every time, so it's ugly.
>>
>> It would probably be too much to add a new kind of function definition,
>> but I was wondering if anybody ever cared about this kind of things. For
>> instance, would people usually expect frameworks/libaries or Javascript
>> itself to not make functions constructible if they're not meant to be?
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170424/ab36fe4d/attachment.html>
More information about the es-discuss
mailing list