Prototypes, inheritance, F.prototype Flashcards

javascript.info Advanced working with functions 6.1 6.2

1
Q

The property _______is internal and hidden, but there are many ways to set it.

A

[[Prototype]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

________ is a historical getter/setter for [[Prototype]]

A

__proto__

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

__proto__ is the same as [[Prototype]]

TRUE / FALSE

A

__proto__ is not the same as [[Prototype]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Object.getPrototypeOf & Object.setPrototypeOf

replaced

A

__proto__ set and get

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
let animal = {
  eats: true
};
let rabbit = {
  jumps: true
};

rabbit.__proto__ = animal; // (*)

// we can find both properties in rabbit now:
alert( rabbit.eats ); //
alert( rabbit.jumps ); //

A

true

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
let animal = {
  eats: true,
  walk() {
    alert("Animal walk");
  }
};
let rabbit = {
  jumps: true,
  \_\_proto\_\_: animal
};

let longEar = {
earLength: 10,
__proto__: rabbit
};

longEar.walk(); //
alert(longEar.jumps); //

A
// walk is taken from the prototype chain
Animal walk
true // (from rabbit)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The value of __proto__ can be either an_____ or ______, other types (like primitives) are ignored.

A

object

null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The prototype is used for reading and writing properties

TRUE / FALSE

A

FLASE

The prototype is only used for reading properties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

this is not affected by prototypes at all

TRUE / FALSE

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

No matter where the method is found: in an object or its prototype. In a method call, _______ is always the object before the dot.

A

“this”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The for..in loops over inherited properties.

TRUE / FALSE

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The prototype is only used for _________ properties.

A

reading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
let animal = {
  walk() {
    if (!this.isSleeping) {
      alert(`I walk`);
    }
  },
  sleep() {
    this.isSleeping = true;
  }
};

let rabbit = {
name: “White Rabbit”,
__proto__: animal
};

rabbit.sleep();
alert(animal.isSleeping); //

A

undefined (no such property in the prototype)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
let animal = {
  eats: true
};
let rabbit = {
  jumps: true,
  \_\_proto\_\_: animal
};

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

A

jumps

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
let animal = {
  eats: true
};
let rabbit = {
  jumps: true,
  \_\_proto\_\_: animal
};

for(let prop in rabbit) alert(prop); //

A

jumps, then eats

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If that’s not what we want, and we’d like to exclude inherited properties, there’s a built-in method __________________ it returns true if obj has its own (not inherited) property named key.

A

obj.hasOwnProperty(key):

17
Q

Remember, new objects can be created with a _________ function.

A

constructor

18
Q
let animal = {
  eats: true
};
function Rabbit(name) {
  this.name = name;
}

Rabbit.prototype = animal;

let rabbit = new Rabbit(“White Rabbit”); // WRITE USING PROTO

alert( rabbit.eats ); // true

A

rabbit.__proto__ == animal

19
Q
let animal = {
  eats: true
};
function Rabbit(name) {
  this.name = name;
}

Rabbit.prototype = animal;

A

prototype

[[prototype]]

20
Q

The default “prototype” is an object with the only property__________ that points back to the function itself.

A

constructor

21
Q

function Rabbit() {}

/* default prototype
Rabbit.prototype = \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
*/

HINT: use constructor

A

{ constructor: Rabbit };

22
Q

function Rabbit() {}

/* default prototype
\_\_\_\_\_\_\_\_\_\_\_\_ = { constructor: Rabbit };
*/

HINT: use prototype

A

Rabbit.prototype

23
Q

function Rabbit() {}

// Rabbit.prototype = { constructor: Rabbit }

alert( __________________); // true

CHECK If RABBIT == constructor

A

Rabbit.prototype.constructor == Rabbit

24
Q

function Rabbit() {}

// Rabbit.prototype = { constructor: Rabbit }

let rabbit = new Rabbit(); // inherits from

A

{constructor: Rabbit}

25
Q

function Rabbit() {}

// Rabbit.prototype = { constructor: Rabbit }

alert(rabbit.constructor == Rabbit); // inherits from

A

(from prototype)

26
Q

function Wabbit() {}

};

let rabbit = new Wabbit();
alert(rabbit.constructor === Wabbit); // 

TRUE / FLASE

A

TRUE

27
Q
function Rabbit() {}
Rabbit.prototype = {
  jumps: true
};
let rabbit = new Rabbit();
alert(rabbit.constructor === Rabbit); //

TRUE / FLASE

A

FLASE

we replace the default prototype as a whole, then there will be no “constructor” in it.

28
Q

The value of F.prototype should be either an _______ or _______: other values won’t work.

A

object

null

29
Q

The only thing F.prototype does: it sets ___________ of new objects when new F() is called.

A

[[Prototype]]

30
Q

By default all functions have F.prototype =

A

{ constructor: F },

31
Q

By default all functions have ____________ = { constructor: F },

A

F.prototype

32
Q
function Rabbit() {}
Rabbit.prototype = {
  eats: true
};

let rabbit = new Rabbit();

alert( rabbit.eats ); //

A

true

33
Q
function Rabbit() {}
Rabbit.prototype = {
  eats: true
};

let rabbit = new Rabbit();

Rabbit.prototype = {};

alert( rabbit.eats ); // ?

A

true

The assignment to Rabbit.prototype sets up [[Prototype]] for new objects, but it does not affect the existing ones.

34
Q
function Rabbit() {}
Rabbit.prototype = {
  eats: true
};

let rabbit = new Rabbit();

Rabbit.prototype.eats = false;

alert( rabbit.eats ); // ?

A

false

Objects are assigned by reference. The object from Rabbit.prototype is not duplicated, it’s still a single object is referenced both by Rabbit.prototype and by the [[Prototype]] of rabbit.
So when we change its content through one reference, it is visible through the other one.

35
Q
function Rabbit() {}
Rabbit.prototype = {
  eats: true
};

let rabbit = new Rabbit();

delete rabbit.eats;

alert( rabbit.eats ); // ?

A

true

All delete operations are applied directly to the object. Here delete rabbit.eats tries to remove eats property from rabbit, but it doesn’t have it. So the operation won’t have any effect.

36
Q
function Rabbit() {}
Rabbit.prototype = {
  eats: true
};

let rabbit = new Rabbit();

delete Rabbit.prototype.eats;

alert( rabbit.eats ); // ?

A

undefined

The property eats is deleted from the prototype, it doesn’t exist any more.

37
Q

TRUE / FALSE
User.prototype.constructor == User ?

function User(name) {
  this.name = name;
}
let user = new User('John');
let user2 = new user.constructor('Pete');

alert( user2.name ); // Pete (worked!)

A

true

38
Q
function User(name) {
  this.name = name;
}
User.prototype = {}; 
let user = new User('John');
let user2 = new user.constructor('Pete');

alert( user2.name ); //

A

undefined

overwrites User.prototype

39
Q

Why user2.name is undefined?
**********

function User(name) {
  this.name = name;
}
User.prototype = {}; // (*)
let user = new User('John');
let user2 = new user.constructor('Pete');

alert( user2.name ); // undefined

A

First, it looks for constructor in user. Nothing.

Then it follows the prototype chain. The prototype of user is User.prototype, and it also has nothing.

The value of User.prototype is a plain object {}, its prototype is Object.prototype. And there is Object.prototype.constructor == Object. So it is used.