Javascript and Ruby have many features in common, including the ability to add functionality to existing objects.
Ruby monkeypatching
Ruby allows you to reopen classes to add new functionality. For example, I can reopen the String class to add a new method, “foo()“:
irb > class String
irb > def foo
irb > "I see you!"
irb > end
irb > end
# => nil
irb > "".class
# => String
irb > "".foo
# => "I see you!"
Javascript augmenting
Javascript allows you to augment existing objects with additional functionality.
> String.prototype.foo = function foo() {
return "I see you!";
}
function foo() {
return "I see you!";
}
> typeof("");
"string"
> "".foo()
"I see you!"