JavaScript Object Methods 2 — Questions and Answers
Question 1: What does `Object.fromEntries()` do?
- Converts an object to an array of entries
- Creates an object from an iterable of key-value pairs (Correct answer)
- Merges multiple objects into one
- Converts a Map to an array
Correct answer: Creates an object from an iterable of key-value pairs
`Object.fromEntries()` transforms a list of key-value pairs (such as from `Object.entries()`, a Map, or any iterable of `[key, value]`) into an object.
`Object.fromEntries([['a', 1], ['b', 2]])` → `{ a: 1, b: 2 }`. `Object.fromEntries(new Map([['x', 10]]))` → `{ x: 10 }`. Common pattern: transform object properties using `Object.entries()` + `map()` + `Object.fromEntries()`: `Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, v * 2]))`.
Question 2: How does the `for...in` loop differ from `for...of` when used with objects?
- They behave identically for objects
- `for...in` iterates over enumerable property names; `for...of` requires an iterable and plain objects are not iterable (Correct answer)
- `for...of` iterates over values; `for...in` iterates over indices
- `for...in` is for arrays; `for...of` is for objects
Correct answer: `for...in` iterates over enumerable property names; `for...of` requires an iterable and plain objects are not iterable
`for...in` iterates over all enumerable string property names, including inherited ones. `for...of` works only on iterables (arrays, Sets, Maps, strings); plain objects are not iterable by default.
`for (const key in obj)` iterates all enumerable own and inherited properties. Use `obj.hasOwnProperty(key)` to filter inherited ones. `for (const val of obj)` throws `TypeError: obj is not iterable` because plain objects don't implement `[Symbol.iterator]`. To iterate object values with `for...of`, use `for (const val of Object.values(obj))`.
Question 3: What is property shorthand in ES6 object literals?
- Using computed property names in brackets
- Omitting the value when variable name matches the property name (Correct answer)
- Defining methods without the function keyword
- Using getters and setters
Correct answer: Omitting the value when variable name matches the property name
Property shorthand allows you to omit the value when the property name matches a variable in scope: instead of `{ name: name, age: age }`, you can write `{ name, age }`.
ES6 property shorthand: `const x = 1, y = 2; const point = { x, y };` is equivalent to `{ x: x, y: y }`. This is heavily used in React (destructuring + shorthand), function returns, and any place you create objects from local variables. Similarly, method shorthand: `{ greet() {} }` instead of `{ greet: function() {} }`.
Question 4: What are computed property names in JavaScript objects?
- Properties whose values are computed at runtime
- Property names defined using expressions inside square brackets (Correct answer)
- Properties inherited from a prototype
- Properties created by `Object.defineProperty()`
Correct answer: Property names defined using expressions inside square brackets
Computed property names allow you to use an expression inside square brackets as a property name in an object literal: `{ [expr]: value }`. The expression is evaluated at runtime to determine the key.
`const key = 'name'; const obj = { [key]: 'Alice' };` → `{ name: 'Alice' }`. `const obj = { ['prop' + 1]: true };` → `{ prop1: true }`. Computed properties are useful in dynamic programming patterns, Redux action creators (`{ [actionType]: handler }`), and when building objects with programmatic keys.
Question 5: What is the purpose of `Object.getPrototypeOf()`?
- Sets an object's prototype to another object
- Returns the prototype (internal `[[Prototype]]`) of the specified object (Correct answer)
- Checks if one object is a prototype of another
- Creates a new object with the given prototype
Correct answer: Returns the prototype (internal `[[Prototype]]`) of the specified object
`Object.getPrototypeOf(obj)` returns the prototype of the specified object — the value of the internal `[[Prototype]]` property. It's the standard way to get the prototype chain.
`Object.getPrototypeOf([])` → `Array.prototype`. `Object.getPrototypeOf(Array.prototype)` → `Object.prototype`. `Object.getPrototypeOf(Object.prototype)` → `null` (top of the chain). Preferred over `obj.__proto__` which is deprecated. The counterpart `Object.setPrototypeOf()` exists but should be avoided for performance reasons.
Question 6: What does `Object.defineProperty()` allow you to do?
- Add a property that can never be enumerated
- Define or modify a property with precise control over its descriptor attributes (Correct answer)
- Create a property that auto-computes its value
- Define a static property on a class
Correct answer: Define or modify a property with precise control over its descriptor attributes
`Object.defineProperty(obj, propName, descriptor)` allows you to add or modify a property with control over its configurability (`configurable`), enumerability (`enumerable`), writability (`writable`), and `value` — or use `get`/`set` accessors.
Descriptor keys: `value`, `writable` (can value change?), `enumerable` (appears in for..in/Object.keys?), `configurable` (can descriptor be changed or property deleted?). Or use `get`/`set` for accessor properties. This is how many built-in properties like `Array.prototype.length` are defined (non-enumerable). New properties default to `false`/`undefined` for all descriptor flags unless specified.
What does `Object.fromEntries()` do?