Lesson 2.2Lesson 8 of 24
Objects and this
Given a business requirement, apply fundamentals of object implementation to solve the business requirement.
Key points
- 1this is set by the call: obj.method() gives obj, a detached method gets undefined in strict code or the global object otherwise.
- 2call and apply run a function now with a chosen this (apply takes an array of arguments); bind returns a new function.
- 3Spread and Object.assign copy only the top level. Nested objects stay shared; Object.assign also changes its first argument.
- 4Object.create links a new object to a prototype. Changes to the prototype are seen later, and Object.keys lists only own properties.
- 5seal blocks adding and deleting, freeze also blocks changes, and both are shallow. defineProperty flags default to false.
What the exam guide checks here
Objective wording from the exam guide (English).
- 2.2.K1Object literals, property shorthand, computed keys, getters and setters
- 2.2.K2this binding: method call, call/apply/bind, arrow functions
- 2.2.K3Prototypes, Object.create, Object.assign, spread copies (shallow)
- 2.2.K4Object.keys/values/entries, freeze, seal, defineProperty
- 2.2.S1Choose the object code that meets a requirement
Lock it in
Answer scenario questions on this topic. Every option comes with an explanation.
Practise 2.2 · 12 questions