Loader vs ES6 Classes

Axel Rauschmayer axel at rauschma.de
Mon Aug 4 07:47:45 PDT 2014


On Aug 4, 2014, at 16:33 , John Barton <johnjbarton at google.com> wrote:

> As far as I can tell you are basically arguing that simple Loader hooks don't need object state. Of course that is true.

No, I’m arguing that Juan’s code is basically “subclassing” a loader, overriding a method and calling that method. ES6 classes seem like a more elegant way of doing this. You’d get as much object state in the subclass as you want.

> And sure we can write code that carefully and cleverly avoids using 'this'. Why? ES6 added classes because this is often the clearest way to structure more complex systems.  
> 
> In my case the LoaderHooks.normalize() function needs to mark names as originating from 'script' rather than 'module' based on the name (eg 'script:' or in my case trailing ',script'.  The marking table needs to be on 'this'.
> 
> I extend LoaderHooks to InterceptOuputLoaderHooks which calls this.onTranscoded() to copy the transcoded results from load to listeners.  But my real point is why should I have to think about 'this' binding in 2015?
> 
> We don't need to use an old school API now, we have ES6.
> 
> jjb
> 
> On Mon, Aug 4, 2014 at 6:29 AM, Axel Rauschmayer <axel at rauschma.de> wrote:
> On Aug 4, 2014, at 15:16 , Juan Ignacio Dopazo <jdopazo at yahoo-inc.com> wrote:
> 
>> In practice we've found that we rarely use the "new Loader(hooks)` option and instead this is more common:
>> 
>> var loader = new Loader();
>> var loaderFetch = loader.fetch.bind(loader);
>> 
>> loader.fetch = function (loadRecord) {
>>   // do something
>>   return loaderFetch(loadRecord);
>> };
> 
> 
> 
> Why not like this then? You’d need (a compiler for) ES6, though.
> 
> ```js
> class MyLoader extends Loader {
>     fetch(loadRecord) {
>         // do something
>         return super(loadRecord);
>     }
> }
> let loader = new MyLoader();
> ```

-- 
Dr. Axel Rauschmayer
axel at rauschma.de
rauschma.de



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140804/f5737f6f/attachment.html>


More information about the es-discuss mailing list