Created
August 12, 2010 00:15
-
-
Save creationix/520067 to your computer and use it in GitHub Desktop.
An experience in making an object library.
This file contains hidden or 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
Object.defineProperty(Object.prototype, "extend", {value: function (obj) { | |
obj.__proto__ = this; | |
return obj; | |
}}); | |
Object.defineProperty(Object.prototype, "new", {value: function () { | |
var obj = Object.create(this); | |
if (obj.initialize) obj.initialize.apply(obj, arguments); | |
return obj; | |
}}); | |
var Rectangle = { | |
initialize: function initialize(width, height) { | |
this.width = width; | |
this.height = height; | |
}, | |
get area() { | |
return this.width * this.height; | |
} | |
}; | |
var Square = Rectangle.extend({ | |
initialize: function initialize(side) { | |
this.width = side; | |
this.height = side; | |
} | |
}); | |
var rect = Rectangle.new(10, 20); | |
console.log(rect.area); | |
var square = Square.new(15); | |
console.log(square.area); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do i copy it correctly?