Permalink
Browse files

Implement a super minimal object library.

This adds Object.prototype.new and Object.prototype.extend.

They make it easy to do prototypal-OO javascript without
ever messing with constructor functions, prototype
properties and "new".
  • Loading branch information...
1 parent 586e0d1 commit d9b237247b1344c8507198e8ca2001437a7565fb @creationix committed Aug 13, 2010
Showing with 18 additions and 0 deletions.
  1. +18 −0 lib/proto.js
View
@@ -49,3 +49,21 @@ if (typeof proto.map !== 'function') {
return accum;
}});
}
+
+// Implement extend for easy prototypal inheritance
+if (typeof proto.extend !== 'function') {
+ Object.defineProperty(Object.prototype, "extend", {value: function extend(obj) {
+ obj.__proto__ = this;
+ return obj;
+ }});
+}
+
+// Implement new for easy self-initializing objects
+if (typeof proto.new !== 'function') {
+ Object.defineProperty(Object.prototype, "new", {value: function new() {
+ var obj = Object.create(this);
+ if (obj.initialize) obj.initialize.apply(obj, arguments);
+ return obj;
+ }});
+}
+

0 comments on commit d9b2372

Please sign in to comment.