Skip to content

Instantly share code, notes, and snippets.

@STRd6
Created April 15, 2011 05:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save STRd6/921183 to your computer and use it in GitHub Desktop.
Save STRd6/921183 to your computer and use it in GitHub Desktop.
It is even more ultimate.
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