Created
March 20, 2014 19:56
-
-
Save slikts/9672473 to your computer and use it in GitHub Desktop.
My least favorite thing about ES5
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
// I want to add multiple properties to a target object in one statement without adding extra variables to the current scope | |
// This can be done in ES6 with Object.assign: | |
var targetObj = {}; | |
Object.assign({ | |
a: 1, | |
b: 2 | |
}, targetObj); | |
// Using ES5 it is impossible without an intermediary variable for the source object: | |
(function() { | |
var sourceObj = { | |
a: 1, | |
b: 2 | |
}; | |
Object.keys(sourceObj).forEach(function(key) { | |
targetObj[key] = sourceObj[key]; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment