Scoped binding of a method to an object

Rick Waldron waldron.rick at gmail.com
Sun Oct 13 14:50:41 PDT 2013


On Sunday, October 13, 2013, Benjamin (Inglor) Gruenbaum wrote:

> 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,
>

What do you mean by wrap? Using this looks like:

let values = new XArray(1,2,3,4);

values.contains(3); // this is the custom "contains"


> not to mention stuff like `.map` or `.reduce` would still return an Array
>

That's incorrect. XArray.prototype.map would return an XArray instance and
reduce always returns what you specify it to return, which can be anything.


Rick


> which would result in more endless wrapping.
>
>
> On Sun, Oct 13, 2013 at 11:53 PM, Rick Waldron <waldron.rick at gmail.com<javascript:_e({}, 'cvml', 'waldron.rick at gmail.com');>
> > wrote:
>
>>
>>
>>
>> On Sun, Oct 13, 2013 at 2:00 PM, Benjamin (Inglor) Gruenbaum <
>> inglor at gmail.com <javascript:_e({}, 'cvml', 'inglor at gmail.com');>> wrote:
>>
>>> Brendan Eich <brendan at mozilla.com <javascript:_e({}, 'cvml',
>>> '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/20131013/95754e9a/attachment.html>


More information about the es-discuss mailing list