Created
January 30, 2011 19:18
-
-
Save NV/803138 to your computer and use it in GitHub Desktop.
Object-Specific Classes in Ruby and JavaScript (via non-standard __proto__)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rectangle = { | |
width: 10, | |
height: 8 | |
} | |
rectangle.__proto__ = { | |
area: function() { | |
return this.width * this.height | |
} | |
} | |
rectangle.area() // 80 | |
Object.keys(rectangle) // ['width', 'height'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rectangle = { | |
:width => 10, | |
:height => 8 | |
} | |
class << rectangle | |
def area | |
self[:width] * self[:height] | |
end | |
end | |
rectangle.area # 80 | |
rectangle.keys # [:width, :height] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment