Created
May 10, 2019 12:04
-
-
Save DaniAkash/02f2e9e01232370172a98eca532900de to your computer and use it in GitHub Desktop.
A javascript object merge operation that will not allow prototype pollution to happen
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
var merge = function(target, source) { | |
for(var attr in source) { | |
if(attr === "__proto__") continue; // Do not merge the property if it's name is __proto__ | |
if(typeof(target[attr]) === "object" && typeof(source[attr]) === "object") { | |
merge(target[attr], source[attr]); | |
} else { | |
target[attr] = source[attr]; | |
} | |
} | |
return target; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment