Loader vs ES6 Classes

Axel Rauschmayer axel at rauschma.de
Mon Aug 4 08:34:38 PDT 2014


We seem to have a different understanding of “object state”. What do you mean by that term?

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

> Neither example binds 'this' assuming the current Loader implementation, so no object state.  Although maybe you are saying your example ought to work, then we agree ;-)
> 
> 
> On Mon, Aug 4, 2014 at 7:47 AM, Axel Rauschmayer <axel at rauschma.de> wrote:
> 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
> 
> 
> 
> 

-- 
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/6cd3aecf/attachment.html>


More information about the es-discuss mailing list