Permalink
Browse files

protochains

  • Loading branch information...
1 parent 89134c9 commit 3a6b95b64dc37e22de4c562c18c125b7eeea2c84 @polotek polotek committed Jan 13, 2012
Showing with 66 additions and 0 deletions.
  1. +66 −0 protochains.js
View
66 protochains.js
@@ -0,0 +1,66 @@
+var slice = Array.prototype.slice;
+
+var mixin = function(target) {
+ var args = slice.call(arguments,1);
+
+ args.forEach(function(p) {
+ if(!p) { return; }
+
+ Object.keys(p)
+ .forEach(function(k) {
+ target[k] = p[k];
+ });
+ });
+
+ return target;
+}
+
+var getOwnDescriptors = function() {
+ var args = slice.call(arguments)
+ , proto = {};
+
+ args.forEach(function(p) {
+ if(!p) { return; }
+
+ Object.getOwnPropertyNames(p)
+ .forEach(function (k) {
+ proto[k] = Object.getOwnPropertyDescriptor(p, k);
+ });
+ });
+
+ return proto;
+}
+
+var extend = function(o, parent, plus) {
+ var proto = getOwnDescriptors(parent, plus);
+
+ Object.keys(proto).forEach(function(k) {
+ Object.defineProperty(o, k, proto[k]);
+ });
+
+ return o;
+}
+
+var create = function(parent, ctor) {
+ var obj = Object.create(parent, {
+ constructor: {
+ value: ctor,
+ enumerable: false,
+ writable: true,
+ configurable: true
+ }
+ });
+
+ mixin(obj, ctor.prototype);
+
+ return Object.create(obj);
+}
+
+var enhance = function(o) {
+ return extend(o, this.prototype)
+}
+
+exports.mixin = mixin;
+exports.extend = extend;
+exports.create = create;
+exports.enhance = enhance;

0 comments on commit 3a6b95b

Please sign in to comment.