Permalink
Browse files

A shim to provide inherits where ES5 is unavailable

  • Loading branch information...
1 parent 15c22be commit 33651f9d024f158c16058ee6db7b21a0d1387c60 @isaacs committed May 9, 2011
Showing with 40 additions and 0 deletions.
  1. +40 −0 inherits-old.js
View
@@ -0,0 +1,40 @@
+// This is a less perfect implementation of the inherits function,
+// designed to work in cases where ES5 is not available.
+//
+// Note that it is a bit longer, and doesn't properly deal with
+// getter/setters or property descriptor flags (enumerable, etc.)
+
+module.exports = inheritsOld
+
+function inheritsOld (c, p, proto) {
+ function F () { this.constructor = c }
+ F.prototype = p.prototype
+ var e = {}
+ for (var i in c.prototype) if (c.prototype.hasOwnProperty(i)) {
+ e[i] = c.prototype[i]
+ }
+ if (proto) for (var i in proto) if (proto.hasOwnProperty(i)) {
+ e[i] = proto[i]
+ }
+ c.prototype = new F()
+ for (var i in e) if (e.hasOwnProperty(i)) {
+ c.prototype[i] = e[i]
+ }
+ c.super = p
+}
+
+// function Child () {
+// Child.super.call(this)
+// console.error([this
+// ,this.constructor
+// ,this.constructor === Child
+// ,this.constructor.super === Parent
+// ,Object.getPrototypeOf(this) === Child.prototype
+// ,Object.getPrototypeOf(Object.getPrototypeOf(this))
+// === Parent.prototype
+// ,this instanceof Child
+// ,this instanceof Parent])
+// }
+// function Parent () {}
+// inheritsOld(Child, Parent)
+// new Child

0 comments on commit 33651f9

Please sign in to comment.