On Mon, Oct 21, 2013 at 9:16 AM, Erik Arvidsson <[email protected]>wrote:
> There is another thread covering this exact topic at the moment. See > "Scooped binding of a method to an object". > Can you clarify the overlap? Thanks! Rick > On Oct 21, 2013 12:49 AM, "Jonathan Barronville" <[email protected]> > wrote: > > > > Hi everyone. > > > > I have a proposal for ES6. I don't know if it's been discussed before, > so if it has, I don't mean to waste your time … hopefully, I'll be directed > to the right place. > > > > One of the most debated JavaScript topics (at least in my experience) is > whether or not one should modify objects that don't belong to them ... some > say it's okay if you "know what you're doing" and others see it as a crime. > > > > I propose we solve this problem by adding "refinements" to ES6. Ruby > v2.0.0 introduced a nice little feature dubbed "refinements". Refinements > allow you to extend other objects for specific modules of code. I know this > isn't a Ruby mailing list, but because the inspiration comes from the Ruby > implementation, an example is in order (feel free to ignore it). > > > > Say you want to modify the `to_str()` method in the built-in String > class in Ruby, the common way to do this is as follows. > > > > ```ruby > > class String > > def to_str() > > "Blah, blah, blah ... this is an example." > > end > > end > > ``` > > > > Very simple ... just re-open the class and create a method with the same > name. However, there's a problem with this, which happens to be the same > problem we have in JavaScript, and that's the fact that we've now just > modified this method permanently for the rest of the execution. > > > > Well, with refinements, the better way of accomplishing the same thing > is as follows. > > > > ```ruby > > module MyLibrary > > refine(String) do > > def to_str() > > "Blah, blah, blah ... this is an example." > > end > > end > > end > > ``` > > > > Now, if you try to send `to_str()` to, say, a string object `"hello"` > (`"hello".to_str()`), you'll get the original `"hello"` as a return value. > In order to use the refinements made to the class, you have to do the > following. > > > > ```ruby > > using MyLibrary > > > > puts("hello".to_str()) > > ``` > > > > Running the code above properly outputs the string `Blah, blah, blah ... > this is an example.`. > > > > Refinements can only be used after a `using` statement and only within > that file. > > > > Given that we're already getting modules in ES6, I believe something > similar to this would be a great addition to the language. > > > > I can definitely work on a more in-depth and detailed proposal, with > specific JavaScript examples, if needed, but I just would like to hear > thoughts around the idea. > > > > Thanks! > > > > - Jonathan Barronville > > @jonathanmarvens > > > > _______________________________________________ > > es-discuss mailing list > > [email protected] > > https://mail.mozilla.org/listinfo/es-discuss > > > > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss > >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

