Skip to content

Instantly share code, notes, and snippets.

@markahesketh
Last active August 30, 2015 21:16
Show Gist options
  • Save markahesketh/309f3085e0e3e8eff250 to your computer and use it in GitHub Desktop.
Save markahesketh/309f3085e0e3e8eff250 to your computer and use it in GitHub Desktop.
Cutting the mustard
// Feature detection
var supports = !!document.querySelector && !!window.addEventListener;
if ( !supports ) return;
// Enabling / hiding content on document load
document.documentElement.className += ' js-component-name';
// Shallow extend
// If a key has another object as its value, the first object's value will be overridden by the second one during the merge.
var extend = function ( objects ) {
var extended = {};
var merge = function (obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
extended[prop] = obj[prop];
}
}
};
merge(arguments[0]);
for (var i = 1; i < arguments.length; i++) {
var obj = arguments[i];
merge(obj);
}
return extended;
};
// Deep extend
// If a key has another object as its value, the first object's value will be combined with the second one during the merge.
var deepExtend = function ( objects ) {
var extended = {};
var merge = function (obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
if ( Object.prototype.toString.call(obj[prop]) === '[object Object]' ) {
extended[prop] = deepExtend(extended[prop], obj[prop]);
}
else {
extended[prop] = obj[prop];
}
}
}
};
merge(arguments[0]);
for (var i = 1; i < arguments.length; i++) {
var obj = arguments[i];
merge(obj);
}
return extended;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment