Last active
August 30, 2015 21:16
-
-
Save markahesketh/309f3085e0e3e8eff250 to your computer and use it in GitHub Desktop.
Cutting the mustard
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
// 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
References:
http://responsivenews.co.uk/post/18948466399/cutting-the-mustard
http://gomakethings.com/ditching-jquery/