Last active
November 24, 2015 03:40
-
-
Save sebdeckers/50ba906df702f77723fa to your computer and use it in GitHub Desktop.
Object.assign()
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
import lib from './library' | |
lib({ | |
foo: false | |
}) |
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
// Exercise: Implement your own version of Object.assign() | |
// Spec: | |
// http://exploringjs.com/es6/ch_oop-besides-classes.html#Object_assign | |
// Hint: Object.keys() | |
function objectAssign (target, ...overrides) { | |
// ... code ... | |
// Cheat: | |
return Object.assign(target, ...overrides) | |
} | |
for (const expectation of [ | |
// Test: Return target | |
() => { | |
const target = {} | |
const result = objectAssign(target) | |
console.log(result === target) | |
}, | |
// Test: Target should be modified | |
() => { | |
const target = {} | |
const override = { foo: 123 } | |
const result = objectAssign(target, override) | |
console.log(typeof result.foo !== 'undefined') | |
}, | |
// Test: Overrides should not be modified | |
() => { | |
const target = { foo: 123 } | |
const override = { bar: 123 } | |
const result = objectAssign(target, override) | |
console.log(typeof override.foo === 'undefined') | |
}, | |
// Test: Sequentially copy from first to last override | |
() => { | |
const target = {} | |
const first = { foo: 123 } | |
const second = { foo: 456 } | |
const result = objectAssign(target, first, second) | |
console.log(result.foo === second.foo) | |
} | |
]) expectation() | |
// Console output: | |
// true | |
// true | |
// true | |
// true |
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
const defaults = { | |
foo: true, | |
bar: 123 | |
} | |
export default function (overrides) { | |
const options = Object.assign({}, defaults, overrides) | |
console.log(options) // has foo from override & bar from defaults | |
console.log(overrides) // unchanged | |
console.log(defaults) // unchanged | |
} |
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
// Using `for ... of` | |
function objectAssign (target, ...overrides) { | |
for (const override of overrides) | |
for (const key of Object.keys(override)) | |
target[key] = override[key] | |
return target | |
} | |
// Using `forEach` | |
function objectAssign (target, ...overrides) { | |
overrides.forEach(override => { | |
Object.keys(override) | |
.forEach(key => { | |
target[key] = override[key] | |
}) | |
}) | |
return target | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment