Prototypes, inheritance, F.prototype Flashcards
javascript.info Advanced working with functions 6.1 6.2 (39 cards)
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}