We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object.assign
spread operator
get
set
Demo 1
Object.defineProperty(Object.prototype, 'foo', { set (value) { console.log('SET', value); }, }); const obj = { foo: 123 }; // obj 自身有一个 foo 属性 const clone = Object.assign({}, obj) // Object.assign 将 obj.foo 写入到 {} 的时候调用了 set
Demo 2
const o = { set foo (value) { console.log('SET', value) } } const clone2 = Object.assign(o, { foo: 123 })
Demo 3
Object.defineProperty(Object.prototype, 'bar', { writable: false, value: 'abc', }); Object.assign({}, { bar: '123' }) // 类似 const o = {}; o.bar = '123' const o = { bar: '123' } { ...o } //
Question: Object.assign 在目标对象上写入属性,spread operator 在目标对象上定义属性 (Object init, __proto__)
__proto__
ES2018: Rest/Spread Properties
The text was updated successfully, but these errors were encountered:
rest operator
No branches or pull requests
Object.assign
以及spread operator
相同点get
方法读取存储属性的值,然后写入到目标对象Object.assign
以及spread operator
不同点spread operator
直接在目标对象定义一个新的属性,Object.assign
通过set
方法往目标对象写入属性(Demo 1, Demo 2)Object.assign
无法往目标对象写入只读属性,但是spread operator
可以Demo 1
Demo 2
Demo 3
Question:
Object.assign
在目标对象上写入属性,spread operator
在目标对象上定义属性 (Object init,__proto__
)ES2018: Rest/Spread Properties
The text was updated successfully, but these errors were encountered: