Skip to content
This repository
Browse code

Move the class_compat.js experiment to a gist.

  • Loading branch information...
commit 33267cc1274b21929f625466f64c9cd6451315fa 1 parent 7f4e660
Tim Caswell authored August 19, 2010

Showing 1 changed file with 0 additions and 31 deletions. Show diff stats Hide diff stats

  1. 31  lib/class_compat.js
31  lib/class_compat.js
... ...
@@ -1,31 +0,0 @@
1  
-var Class = require('./class');
2  
-
3  
-// This global modifier allows you to treat constructor based classes as if
4  
-// they were prototype based.
5  
-
6  
-// Define an extend method for constructor functions that works like prototype extends.
7  
-Object.defineProperty(Function.prototype, "extend", {value: function extend(obj) {
8  
-  // Clone the functions's prototype
9  
-  var props = {}, proto = this.prototype;
10  
-  Object.getOwnPropertyNames(proto).forEach(function (key) {
11  
-    props[key] = Object.getOwnPropertyDescriptor(proto, key);
12  
-  });
13  
-  
14  
-  // Put the constructor on a prop too
15  
-  props.constructor = {value: this};
16  
-  
17  
-  // Make obj's parent be an Classy version of props
18  
-  if (obj === undefined) return Object.create(Class, props);
19  
-  obj.__proto__ = Object.create(Class, props);
20  
-  Object.freeze(obj);
21  
-  return obj;
22  
-}});
23  
-
24  
-// This is the "new" keyword implemented in pure ES5
25  
-// I added sealing on top of what "new" does
26  
-Object.defineProperty(Function.prototype, "new", {value: function () {
27  
-  var obj = Object.create(this.prototype, {constructor: this});
28  
-  var result = this.apply(obj, arguments);
29  
-  Object.seal(obj);
30  
-  return result === undefined ? obj : result;
31  
-}});

0 notes on commit 33267cc

Please sign in to comment.
Something went wrong with that request. Please try again.