inherits-ex
Advanced tools
Comparing version 1.0.6 to 1.0.7
var inherits = require('./inherits'); | ||
//make sure the aClass.prototype hook to the aObject instance. | ||
var getPrototypeOf = Object.getPrototypeOf; | ||
if (!getPrototypeOf) { | ||
getPrototypeOf = function(obj) { | ||
return obj.__proto__; | ||
}; | ||
} | ||
var setPrototypeOf = Object.setPrototypeOf; | ||
if (!setPrototypeOf) { | ||
setPrototypeOf = function(obj, prototype) { | ||
obj.__proto__ = prototype; | ||
}; | ||
} | ||
module.exports = function(aObject, aClass) { | ||
// ES6: Object.getPrototypeOf / Object.setPrototypeOf | ||
var vOldProto = aObject.__proto__; | ||
var vOldProto = getPrototypeOf(aObject); | ||
var result = false; | ||
if ( vOldProto !== aClass.prototype) { | ||
inherits(aClass, vOldProto.constructor); | ||
aObject.__proto__ = aClass.prototype; | ||
setPrototypeOf(aObject, aClass.prototype); | ||
result = true; | ||
} | ||
return result; | ||
} | ||
}; |
@@ -32,15 +32,44 @@ var inherits = require('./inherits'); | ||
* | ||
mixin the exists method: the new mixin method will oerwrite the old one. | ||
```coffee | ||
class Root | ||
m: -> console.log 'root' | ||
m: -> | ||
console.log 'root' | ||
console.log '----' | ||
class C | ||
m: -> | ||
console.log "c" | ||
super | ||
inherits C, Root | ||
m: -> | ||
console.log "c" | ||
super | ||
class B | ||
inherits B, Root | ||
m: -> | ||
console.log "b" | ||
super | ||
class B11 | ||
m: -> | ||
console.log 'b11' | ||
super | ||
inherits B11, B | ||
m: -> | ||
console.log 'b11' | ||
super | ||
b = new B11 | ||
b.m() | ||
mixin B11, C | ||
b = new B11 | ||
b.m() | ||
# The console results: | ||
# b11 | ||
# b | ||
# root | ||
# ---- | ||
# b11 | ||
# c | ||
# root | ||
# ---- | ||
``` | ||
* | ||
@@ -47,0 +76,0 @@ */ |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/snowyu/inherits-ex.js", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"author": { | ||
@@ -8,0 +8,0 @@ "name": "Riceball LEE", |
var inherits = require('./inherits'); | ||
//make sure the aClass.prototype hook to the aObject instance. | ||
var getPrototypeOf = Object.getPrototypeOf; | ||
if (!getPrototypeOf) { | ||
getPrototypeOf = function(obj) { | ||
return obj.__proto__; | ||
}; | ||
} | ||
var setPrototypeOf = Object.setPrototypeOf; | ||
if (!setPrototypeOf) { | ||
setPrototypeOf = function(obj, prototype) { | ||
obj.__proto__ = prototype; | ||
}; | ||
} | ||
module.exports = function(aObject, aClass) { | ||
// ES6: Object.getPrototypeOf / Object.setPrototypeOf | ||
var vOldProto = aObject.__proto__; | ||
var vOldProto = getPrototypeOf(aObject); | ||
var result = false; | ||
if ( vOldProto !== aClass.prototype) { | ||
inherits(aClass, vOldProto.constructor); | ||
aObject.__proto__ = aClass.prototype; | ||
setPrototypeOf(aObject, aClass.prototype); | ||
result = true; | ||
} | ||
return result; | ||
} | ||
}; |
@@ -32,15 +32,44 @@ var inherits = require('./inherits'); | ||
* | ||
mixin the exists method: the new mixin method will oerwrite the old one. | ||
```coffee | ||
class Root | ||
m: -> console.log 'root' | ||
m: -> | ||
console.log 'root' | ||
console.log '----' | ||
class C | ||
m: -> | ||
console.log "c" | ||
super | ||
inherits C, Root | ||
m: -> | ||
console.log "c" | ||
super | ||
class B | ||
inherits B, Root | ||
m: -> | ||
console.log "b" | ||
super | ||
class B11 | ||
m: -> | ||
console.log 'b11' | ||
super | ||
inherits B11, B | ||
m: -> | ||
console.log 'b11' | ||
super | ||
b = new B11 | ||
b.m() | ||
mixin B11, C | ||
b = new B11 | ||
b.m() | ||
# The console results: | ||
# b11 | ||
# b | ||
# root | ||
# ---- | ||
# b11 | ||
# c | ||
# root | ||
# ---- | ||
``` | ||
* | ||
@@ -47,0 +76,0 @@ */ |
51499
47
821