Objects & Prototypes
Difficulty: medium
Overview
Objects are key-value stores. Properties can be accessed via dot or bracket notation. Computed property keys allow dynamic key names. Shorthand properties omit the colon when key and variable names match. Object.keys(), Object.values(), Object.entries() return arrays. Object.assign() and the spread operator {...obj} create shallow copies. Every object has a [[Prototype]] chain resolved at runtime.
Practice Linked Questions
Q1. What is the output? ```js const obj = { a: 1 }; const obj2 = obj; obj2.a = 99; console.log(obj.a); ```
Select one answer before revealing.
Q2. What is the output? ```js const obj = { x: 1, y: 2 }; const { x, z = 10 } = obj; console.log(x, z); ```
Select one answer before revealing.
Q3. What does `Object.keys({ a: 1, b: 2 })` return?
Select one answer before revealing.
Q4. What is optional chaining (`?.`) used for?
Select one answer before revealing.
Q5. What is the output? ```js const user = null; console.log(user?.name ?? "Guest"); ```
Select one answer before revealing.
Q6. What is the output? ```js const obj = { name: "Alice" }; function greet() { return `Hello, ${this.name}`; } console.log(greet.call(obj)); ```
Select one answer before revealing.
Q7. What does `Function.prototype.bind()` return?
Select one answer before revealing.
Q8. What does the `new` keyword do when used with a constructor function?
Select one answer before revealing.
Q9. Which statements about `Object.assign()` are correct? (More than one answer may be correct.)
Select one answer before revealing.
Q10. What does the `for...in` loop iterate over?
Select one answer before revealing.
Q11. What is the output? ```js function Person(name) { this.name = name; } const p = new Person("Bob"); console.log(p.name); ```
Select one answer before revealing.
Q12. What is the output? ```js const obj = { a: 1, b: 2, c: 3 }; const result = Object.entries(obj).map(([k, v]) => `${k}:${v}`); console.log(result); ```
Select one answer before revealing.
Q13. Which of the following are valid ways to create a copy of an object without mutating the original? (More than one answer may be correct.)
Select one answer before revealing.