Skip to content

Commit 12d6162

Browse files
committed
Added aside mentioning Object.assign() and accessor properties (fixes nzakas#66)
1 parent 3a0c2da commit 12d6162

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

manuscript/03-Objects.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,28 @@ The value of `receiver.type` is `"css"` because the second supplier overwrote th
209209

210210
The `Object.assign()` method isn't a big addition to ECMAScript 6, but it does formalize a common function that is found in many JavaScript libraries.
211211

212+
A> ### Working with Accessor Properties
213+
A>
214+
A> Keep in mind that you cannot create accessor properties on the receiver by using a supplier with accessor properties. Since `Object.assign()` uses the assignment operator, an accessor property on a supplier will become a data property on the receiver. For example:
215+
A>
216+
A> ```js
217+
A> var receiver = {},
218+
A> supplier = {
219+
A> get name() {
220+
A> return "file.js"
221+
A> }
222+
A> };
223+
A>
224+
A> Object.assign(receiver, supplier);
225+
A>
226+
A> var descriptor = Object.getOwnPropertyDescriptor(receiver, "name");
227+
A>
228+
A> console.log(descriptor.value); // "file.js"
229+
A> console.log(descriptor.get); // undefined
230+
A> ```
231+
A>
232+
A> In this code, the `supplier` has an accessor property called `name`. After using `Object.assign()`, `receiver.name` exists as a data property with the value of `"file.js"`. That's because `supplier.name` returned "file.js" at the time `Object.assign()` was called.
233+
212234

213235
## __proto__, Object.setPrototypeOf()
214236

0 commit comments

Comments
 (0)