Created
November 23, 2018 15:55
-
-
Save miladvafaeifard/9a3e7f9f3c34325739b7935158061d6d to your computer and use it in GitHub Desktop.
object appends chains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.appendChain = function (oChain, oProto) { | |
if (typeof oProto !== 'object' && typeof oProto !== 'string') { | |
throw new TypeError('second argument must be an object or a string'); | |
} | |
var oNewProto = oProto; | |
var oReturn = o2nd = oLast = oChain instanceof this ? oChain : new oChain.constructor(oChain); | |
for (var o1st = this.getPrototypeOf(o2nd); o1st !== Object.prototype && o1st !== Function.prototype; o1st = this.getPrototypeOf(o2nd)) { | |
o2nd = o1st; | |
} | |
if (oProto.constructor === String) { | |
oNewProto = Function.prototype; | |
oReturn = Function.apply(null, Array.prototype.slice.call(arguments, 1)); | |
this.setPrototypeOf(oReturn, oLast); | |
} | |
this.setPrototypeOf(o2nd, oNewProto); | |
return oReturn; | |
}; | |
function Mammal() { | |
this.isMammal = 'yes'; | |
} | |
function MammalSpecies(sMammalSpecies) { | |
// Mammal.call(this); | |
this.species = sMammalSpecies; | |
} | |
MammalSpecies.prototype = new Mammal(); | |
MammalSpecies.prototype.constructor = MammalSpecies; | |
var oCat = new MammalSpecies('Felis'); | |
function Animal() { | |
this.breathing = 'yes'; | |
} | |
let ssss = Object.appendChain(oCat, new Animal()); | |
console.log('cat: ', oCat.breathing); | |
console.log('cat: ', oCat.isMammal); | |
// ssss(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment