Scoped binding of a method to an object

Benjamin (Inglor) Gruenbaum inglor at gmail.com
Sun Oct 13 14:46:07 PDT 2013


This sort of decoration is nice and it's how I usually do this right now,
but this requires me to wrap every array with an `XArray` call which is
tedious, not to mention stuff like `.map` or `.reduce` would still return
an Array which would result in more endless wrapping.


On Sun, Oct 13, 2013 at 11:53 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

>
>
>
> On Sun, Oct 13, 2013 at 2:00 PM, Benjamin (Inglor) Gruenbaum <
> inglor at gmail.com> wrote:
>
>> Brendan Eich <brendan at mozilla.com> wrote:
>> > No, object detection, polyfilling, and even "prollyfilling" are common
>> and successful adaptationsp on the Web.
>>
>> Polyfilling is great _after_ the method has already been added to the
>> spec. I'm completely fine with adding an Array.prototype.map shim to IE8,
>> the problem with adding a method that's not on the prototype yet is that
>> it'll fail in case the spec is different from the implementation I chose.
>> If you mentioned PrototypeJS, its `.bind` method is one such example.
>>
>> > Your subject recalls a defunct proposal to add lexically-scoped but
>> heap-based -- therefore object property-lookup performance hindering --
>> extension properties.
>>
>> I have to say this surprises me, a performance issue is the last thing I
>> expected. What about attaching a prototype as a closure variable, something
>> (and this is a syntax I __don't__ like) like:
>> ```
>> (function(use Array){
>>     Array.prototype.contains = function() { ...
>>     ...
>>     // any code here has access to .contains, code that did not originate
>> here does not have such access, much like a closure.
>>     // other code is free to use Array without any collisions.
>> })()
>> ```
>>
>
> Another solution is creating your own subclass with custom methods...
>
>
> class XArray extends Array {
>    constructor(...args) {
>      super(...args);
>    }
>    contains(arg) { ... }
> }
>
>
> Then all code in this application uses XArray and gets its custom
> "contains"
>
>
> Rick
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131014/0ba2aa11/attachment.html>


More information about the es-discuss mailing list