Inheritance and the Prototype Chain JS Quiz
by Kwiz
Qs
27
Difficulty
Senior
Plays
0
Record
n/a
Description

One of the biggest initial barriers for getting comfortable with JavaScript is understanding that it isn't a class-based language, but mimics inheritance via the prototype chain. This JavaScript quiz covers the sometimes confusing prototype chain and sequence of inheritance.

It's a tough one, so only truly dedicated JS coders will be able to get 100%...

References for this quiz can be found at:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain https://javascript.info/function-prototype

Question Preview
When trying to access a property of an object, the property will not only be sought on the object but on the ______ of the object.
Question Preview
Which method can be used to access the prototype of an object?
Question Preview
What will the output be?
let f = function () {
   this.a = 1;
   this.b = 2;
}

let o = new f();

f.prototype.b = 3;
f.prototype.c = 4;

console.log(o.a); // ?
Question Preview
What will the output be?
let f = function () {
   this.a = 1;
   this.b = 2;
}

let o = new f();

f.prototype.b = 3;
f.prototype.c = 4;

console.log(o.d); // ?
Question Preview
Setting a property to an object creates an ______ property.
Question Preview
What is the output of the following code?
let o = {
  a: 2,
  m: function() {
    return this.a + 1;
  }
};

let p = Object.create(o);

p.a = 4;

console.log(p.m()); // ?
Question Preview
Aside from arrow functions, all functions in JavaScript have a special property named _________
Question Preview
What keyword do we use to create an instance of doSomething?
function doSomething(){}
doSomething.prototype.foo = "bar";
const instanceOfDoSomething = /* ???? */ doSomething();
Question Preview
What is the output of this code?
function doSomething() {}
doSomething.prototype.foo = "bar";
let doSomethingInstance = new doSomething();
doSomethingInstance.prop = "some value";

console.log(doSomething.prop); // ?
Question Preview
What is the output of the following code?
function doSomething() {}
doSomething.prototype.foo = "bar";
let doSomeInstancing = new doSomething();
doSomeInstancing.prop = "some value";

console.log("doSomething.prototype.foo: " + doSomething.prototype.foo); // ?
Question Preview
What is the [[Prototype]] of the following object?
let o = { a: 1 };
Question Preview
What is the prototype of Object.prototype?
Question Preview
What does the prototype chain of the following code look like?
let b = ["hello", "world"];
Question Preview
A "constructor" in JavaScript is "just" a function that happens to be called with the _____ operator.
function Graph() {
  this.vertices = [];
  this.edges = [];
}

Graph.prototype = {
  addVertex: function(v) {
    this.vertices.push(v);
  }
};

let g = /* ???? */ Graph();
Question Preview
What is the output of the following code?
let o = Object.create(null);
console.log(o.hasOwnProperty); // ?
Question Preview
What does the prototype chain of b look like?
let a = { a: 1 };

let b = Object.create(a);
Question Preview
What is the first argument to Object.create?
Question Preview
Which keyword is NOT involved in implementing an ES2015+ class
Question Preview
The following code is valid
class Square extends Polygon {
  constructor(sideLength) {
    super(sideLength, sideLength);
  }
  get area() {
    return this.height * this.width;
  }
  set sideLength(newLength) {
    this.height = newLength;
    this.width = newLength;
  }
}

var square = new Square(2);
Question Preview
When iterating over the properties of an object, every enumerable property that is on the prototype chain will be enumerated.
Question Preview
The hasOwnProperty method traverses the prototype chain
Question Preview
Which of the following is NOT valid for extending the prototype chain?
Question Preview
The [[Prototype]] property can be directly referenced
Question Preview
__proto__ is the same as the internal [[Prototype]] property
Question Preview
In a method call, this is always the object ______ the dot.
Question Preview
What will be printed to the console?
let animal = {
  eats: true
};

let rabbit = {
  jumps: true,
  __proto__: animal
};

alert(Object.keys(rabbit)); // jumps

for(let prop in rabbit) {
  alert(prop); // ?
}
Question Preview
If we call rabbit.eat(), which object receives the full property: animal or rabbit?
let animal = {
  eat() {
    this.full = true;
  }
};

let rabbit = {
  __proto__: animal
};

rabbit.eat(); // ?