raganwald.github.com
raganwald.github.com copied to clipboard
Extension methods don't need to be static
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.