Created
June 14, 2014 19:54
-
-
Save AverageMarcus/6ab6fbc57fe97be91c7a to your computer and use it in GitHub Desktop.
Extends an object with values from another object. Existing properties are ignored. Useful for ensuring options have default values.
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.prototype.extend = function(obj){ | |
if(typeof obj !== 'object') return this; | |
var current, prop, | |
i = 0, | |
length = arguments.length; | |
for(; i<length; i++){ | |
current = arguments[i]; | |
if(typeof current === 'object'){ | |
for(prop in current){ | |
if(!this[prop]){ | |
this[prop] = current[prop]; | |
} | |
} | |
}else{ | |
console.warn("Input variables should be objects, ignoring."); | |
} | |
} | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: