Prototypes, inheritance, F.prototype Flashcards
javascript.info Advanced working with functions 6.1 6.2
The property _______is internal and hidden, but there are many ways to set it.
[[Prototype]]
________ is a historical getter/setter for [[Prototype]]
__proto__
__proto__ is the same as [[Prototype]]
TRUE / FALSE
__proto__ is not the same as [[Prototype]]
Object.getPrototypeOf & Object.setPrototypeOf
replaced
__proto__ set and get
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 ); //
true
true
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); //
// walk is taken from the prototype chain Animal walk true // (from rabbit)
The value of __proto__ can be either an_____ or ______, other types (like primitives) are ignored.
object
null
The prototype is used for reading and writing properties
TRUE / FALSE
FLASE
The prototype is only used for reading properties
this is not affected by prototypes at all
TRUE / FALSE
true
No matter where the method is found: in an object or its prototype. In a method call, _______ is always the object before the dot.
“this”
The for..in loops over inherited properties.
TRUE / FALSE
true
The prototype is only used for _________ properties.
reading
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); //
undefined (no such property in the prototype)
let animal = { eats: true };
let rabbit = { jumps: true, \_\_proto\_\_: animal };
alert(Object.keys(rabbit)); //
jumps
let animal = { eats: true };
let rabbit = { jumps: true, \_\_proto\_\_: animal };
for(let prop in rabbit) alert(prop); //
jumps, then eats
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.
obj.hasOwnProperty(key):
Remember, new objects can be created with a _________ function.
constructor
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
rabbit.__proto__ == animal
let animal = { eats: true };
function Rabbit(name) { this.name = name; }
Rabbit.prototype = animal;
prototype
[[prototype]]
The default “prototype” is an object with the only property__________ that points back to the function itself.
constructor
function Rabbit() {}
/* default prototype Rabbit.prototype = \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ */
HINT: use constructor
{ constructor: Rabbit };
function Rabbit() {}
/* default prototype \_\_\_\_\_\_\_\_\_\_\_\_ = { constructor: Rabbit }; */
HINT: use prototype
Rabbit.prototype
function Rabbit() {}
// Rabbit.prototype = { constructor: Rabbit }
alert( __________________); // true
CHECK If RABBIT == constructor
Rabbit.prototype.constructor == Rabbit
function Rabbit() {}
// Rabbit.prototype = { constructor: Rabbit }
let rabbit = new Rabbit(); // inherits from
{constructor: Rabbit}
function Rabbit() {}
// Rabbit.prototype = { constructor: Rabbit }
alert(rabbit.constructor == Rabbit); // inherits from
(from prototype)
function Wabbit() {}
};
let rabbit = new Wabbit(); alert(rabbit.constructor === Wabbit); //
TRUE / FLASE
TRUE
function Rabbit() {} Rabbit.prototype = { jumps: true };
let rabbit = new Rabbit(); alert(rabbit.constructor === Rabbit); //
TRUE / FLASE
FLASE
we replace the default prototype as a whole, then there will be no “constructor” in it.
The value of F.prototype should be either an _______ or _______: other values won’t work.
object
null
The only thing F.prototype does: it sets ___________ of new objects when new F() is called.
[[Prototype]]
By default all functions have F.prototype =
{ constructor: F },
By default all functions have ____________ = { constructor: F },
F.prototype
function Rabbit() {} Rabbit.prototype = { eats: true };
let rabbit = new Rabbit();
alert( rabbit.eats ); //
true
function Rabbit() {} Rabbit.prototype = { eats: true };
let rabbit = new Rabbit();
Rabbit.prototype = {};
alert( rabbit.eats ); // ?
true
The assignment to Rabbit.prototype sets up [[Prototype]] for new objects, but it does not affect the existing ones.
function Rabbit() {} Rabbit.prototype = { eats: true };
let rabbit = new Rabbit();
Rabbit.prototype.eats = false;
alert( rabbit.eats ); // ?
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.
function Rabbit() {} Rabbit.prototype = { eats: true };
let rabbit = new Rabbit();
delete rabbit.eats;
alert( rabbit.eats ); // ?
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.
function Rabbit() {} Rabbit.prototype = { eats: true };
let rabbit = new Rabbit();
delete Rabbit.prototype.eats;
alert( rabbit.eats ); // ?
undefined
The property eats is deleted from the prototype, it doesn’t exist any more.
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!)
true
function User(name) { this.name = name; } User.prototype = {};
let user = new User('John'); let user2 = new user.constructor('Pete');
alert( user2.name ); //
undefined
overwrites User.prototype
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
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.