JavaScript Prototypes and Classes 5 — Questions and Answers
Question 1: What does `Object.create(null)` produce?
- An object whose prototype is Object.prototype
- null
- An object with no prototype at all (Correct answer)
- An empty array
Correct answer: An object with no prototype at all
`Object.create(null)` creates a truly empty object with no `[[Prototype]]`, so it doesn't inherit from `Object.prototype`.
Question 2: In a class, what is the difference between a getter defined inside and a property assigned in the constructor?
- No difference; both are placed on the instance
- Getters go on the prototype; constructor assignments go on the instance (Correct answer)
- Getters go on the instance; constructor assignments go on the prototype
- Both go on the prototype
Correct answer: Getters go on the prototype; constructor assignments go on the instance
Getters/setters defined in a class body are placed on `ClassName.prototype`, while properties assigned via `this.x = ...` in the constructor are own properties of each instance.
Question 3: What is 'prototype pollution' in JavaScript?
- Adding too many methods to a class
- Maliciously modifying Object.prototype to affect all objects (Correct answer)
- Overriding a child class method
- Using mixins with conflicting method names
Correct answer: Maliciously modifying Object.prototype to affect all objects
Prototype pollution is a vulnerability where an attacker modifies `Object.prototype`, causing all objects to inherit malicious properties.
Question 4: What is the output of `console.log([] instanceof Array)` in JavaScript?
- false
- true (Correct answer)
- TypeError
- undefined
Correct answer: true
`Array.prototype` is in the prototype chain of any array literal, so `instanceof Array` returns `true`.
Question 5: How can you call a parent class static method from a subclass static method?
- this.methodName()
- super.methodName() (Correct answer)
- Parent.prototype.methodName()
- base.methodName()
Correct answer: super.methodName()
Inside a subclass static method, `super.methodName()` correctly calls the parent class's static method.
Question 6: What is 'shadowing' in the context of JavaScript prototype chains?
- Making a prototype property non-enumerable
- Creating an own property on an instance that hides a same-named prototype property (Correct answer)
- Hiding private fields from subclasses
- Freezing inherited methods
Correct answer: Creating an own property on an instance that hides a same-named prototype property
When you assign a property directly on an object that also exists on its prototype, the own property 'shadows' (hides) the inherited one.
Question 7: What is the role of `Symbol.hasInstance` in JavaScript?
- It defines a custom iterator for the class
- It overrides how `instanceof` behaves for the class (Correct answer)
- It marks a method as private
- It creates a singleton pattern
Correct answer: It overrides how `instanceof` behaves for the class
Defining `static [Symbol.hasInstance](instance)` on a class lets you customize how `instanceof` checks work for that class.
What does `Object.create(null)` produce?