Prefer using an object spread over `Object.assign`

이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: javascript-best-practices/prefer-object-spread

Language: JavaScript

Severity: Warning

Category: Performance

Description

This rule encourages the use of the object spread syntax over the Object.assign method when creating a new object from an existing one where the first argument is an empty object. This is because the object spread syntax is more concise, easier to read, and can eliminate the need for null checks that are often necessary with Object.assign.

If you need to use Object.assign, make sure that the first argument is not an object literal, as this can easily be replaced with the spread syntax.

Non-Compliant Code Examples

Object.assign({}, foo);

Object.assign({}, {foo: 'bar'});

Object.assign({ foo: 'bar'}, baz);

Object.assign({}, baz, { foo: 'bar' });

Object.assign({}, { ...baz });

Object.assign({});

Object.assign({ foo: bar });

Compliant Code Examples

({ ...foo });

({ ...baz, foo: 'bar' });

Object.assign(foo, { bar: baz });

Object.assign(foo, bar);

Object.assign(foo, { bar, baz });

Object.assign(foo, { ...baz });
PREVIEWING: brett.blue/embedded-collector-release