Extension methods don't need to be static #57

Open
Suor opened this Issue Oct 8, 2015 · 0 comments

Projects

None yet

1 participant

@Suor
Suor commented Oct 8, 2015

It could be done dynamically like:

let String.prototype.reverse = function (...) {...};

'abc'.reverse()
getString().reverse()

In second example JavaScript implementation can't know statically whether getString() will return string, but it will now when it will get to it. This is actually better than :: cause you can polymorph:

let String.prototype.reverse = function (...) {...};
let Array.prototype.reverse = function (...) {...};
// ...
something.reverse()

The key idea is that extension is lexically scoped so that it won't affect any random code. Also, you most surely will want to import/export this extensions so my primitive syntax might not work, but this is only syntax issue.

There are actually a corresponding feature in ruby 2.0 and a perl module for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment