Can you do operator overloading in Javascript?Originally Posted by Maian
Another Ruby example:
Sure you could use .add() to get the same effect, or you could use functions and an associative array instead if you like, all the same result.Code:class Putty attr_reader :weight def initialize(weight = 10) @weight = weight end def + (more_putty) Putty.new(@weight + more_putty.weight) end end first_blob = Putty.new #=> @weight = 10 second_blob = Putty.new #=> @weight = 10 big_blob = first_blob + second_blob big_blob #=> @weight = 20
Though a + b looks nicer than a.add(b) or add(a, b) or (add a b) imo
Douglas






. Even better would be probably

Bookmarks