-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Closed
Description
The Object.assign example is currently incorrect.
function createMenu(config) {
Object.assign(config, {
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
});
}
createMenu({ title: 'Not Foo' });The title property will always be set to 'Foo'!
I was going to make a PR with a fix, but the approach really depends on whether the mutation of config is desirable.
How I would do it (this does not behave identically to the "bad" example):
function createMenu(config) {
// makes a copy of `config`, with default values
config = Object.assign({
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
}, config);
}How you could do it with mutation (this is identical to the "bad" example):
function createMenu(config) {
// mutates `config`, setting default values
Object.assign(config, Object.assign({
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
}, config));
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels