Created
April 15, 2011 05:25
-
-
Save STRd6/921183 to your computer and use it in GitHub Desktop.
It is even more ultimate.
This file contains 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.extend = function(destination, source) { | |
for (var property in source) { | |
destination[property] = source[property]; | |
} | |
return destination; | |
}; | |
Object.reverseMerge = function(destination, source) { | |
for (var key in source) { | |
if(!(key in destination)) { | |
destination[key] = value; | |
} | |
} | |
return destination; | |
} | |
function Person(I) { | |
I = I || {}; | |
Object.reverseMerge(I, { | |
name: "McLovin", | |
age: 25, | |
homeState: "Hawaii" | |
}); | |
return { | |
I: I, | |
introduce: function() { | |
return "Hi I'm " + I.name + " and I'm " + I.age; | |
} | |
}; | |
} | |
var fogel = Person({ | |
age: "old enough" | |
}); | |
fogel.introduce(); // "Hi I'm McLovin and I'm old enough" | |
function Ninja(I) { | |
I = I || {}; | |
Object.reverseMerge(I, { | |
belt: "black" | |
}); | |
return Object.extend(Person(I), { | |
greetChallenger: function() { | |
return "In all my " + I.age + " years as a ninja, I've never met a challenger as worthy as you..."; | |
} | |
}); | |
} | |
var resig = Ninja({name: "John Resig"}); | |
resig.introduce(); // "Hi I'm John Resig and I'm 25" | |
// Save / Load | |
Ninja(JSON.parse(JSON.stringify(resig.I)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment