raganwald.github.com icon indicating copy to clipboard operation
raganwald.github.com copied to clipboard

Extension methods don't need to be static

Open Suor opened this issue 10 years ago • 0 comments

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.

Suor avatar Oct 08 '15 04:10 Suor