Skip to content

Instantly share code, notes, and snippets.

@brandonaaron
Forked from creationix/simpleobj.js
Created August 12, 2010 00:28
Show Gist options
  • Save brandonaaron/520079 to your computer and use it in GitHub Desktop.
Save brandonaaron/520079 to your computer and use it in GitHub Desktop.
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);
@brianleroux
Copy link

Ruby much?

@creationix
Copy link

Yeah, but that's wasn't the intent. Maybe setup instead of initialize. I just want something short to type and easy to remember.

@brianleroux
Copy link

Naw, I like it actually. Tho you can't tell me this wouldn't confuse a n00b esp one w/ a little ruby experience. My experience is that ppl are always confused anyhow. Fuck it. Use the language. The pretty stuff. The warts. All of it. Thats what its there for.

@creationix
Copy link

Sounds good, at least it works mostly how ruby works so there are no surprises there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment