Native Prototypes Flashcards
(40 cards)
let obj = {}; alert( obj ); //
“[object Object]” ?
obj = {} is the same as obj = new Object()
TRUE / FALSE
TRUE
When new Object() is called (or a literal object {…} is created), the [[Prototype]] of it is set to ________
object.prototype
when obj.toString() is called the method is taken from ____________
Object.prototype.
let arr = [1, 2, 3];
// it inherits from Array.prototype? alert( \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ); // true
arr.__proto__ === Array.prototype
let arr = [1, 2, 3];
// it inherits from Array.prototype? alert( arr.\_\_proto\_\_ === Array.prototype ); // ?
true
let arr = [1, 2, 3];
// then from Object.prototype? alert( \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ); // true
arr.__proto__.__proto__ === Object.prototype
let arr = [1, 2, 3];
// then from Object.prototype? alert( arr.\_\_proto\_\_.\_\_proto\_\_ === Object.prototype ); //
true
let arr = [1, 2, 3];
// and null on the top. alert( \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_); // null
arr.__proto__.__proto__.__proto__
let arr = [1, 2, 3];
// and null on the top. alert( arr.\_\_proto\_\_.\_\_proto\_\_ ); //
[[object Object]]
function f() {}
alert(f.__proto__.__proto__ == Function.prototype); //
false
function f() {}
alert(f.__proto__. == Function.prototype); //
true
function f() {}
alert(f.__proto__.__proto__ == Object.prototype); //
true, inherit from objects
Values _______ and _________ have no object wrappers
null
undefined
_________ is a term for making a substitute for a method that exists in JavaScript specification, but not yet supported by current JavaScript engine
Polyfilling
The __proto__ is considered outdated and somewhat deprecated (in browser-only part of the JavaScript standard).
The modern methods are:
Object.create(proto[, descriptors]) – creates an empty object with given proto as [[Prototype]] and optional property descriptors.
Object.getPrototypeOf(obj) – returns the [[Prototype]] of obj.
Object.setPrototypeOf(obj, proto) – sets the [[Prototype]] of obj to proto.
Object.create(proto[, descriptors])
Does two things
- creates a new object with assinged prototype
2. gives optional property descriptors. We can provide additional properties to the new object there
// fully identical shallow clone of obj let clone =
Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
The __proto__ property is special: it must be either an _____ or _______
object
null
__________ creates an empty object without a prototype
Object.create(null)
function Rabbit(name) { this.name = name; } Rabbit.prototype.sayHi = function() { alert( this.name ); }
let rabbit = new Rabbit(“Rabbit”);
rabbit.sayHi(); //
Rabbit
function Rabbit(name) { this.name = name; } Rabbit.prototype.sayHi = function() { alert( this.name ); }
let rabbit = new Rabbit(“Rabbit”);
Rabbit.prototype.sayHi(); //
undefined
function Rabbit(name) { this.name = name; } Rabbit.prototype.sayHi = function() { alert( this.name ); }
let rabbit = new Rabbit(“Rabbit”);
Object.getPrototypeOf(rabbit).sayHi(); //
undefined
function Rabbit(name) { this.name = name; } Rabbit.prototype.sayHi = function() { alert( this.name ); }
let rabbit = new Rabbit(“Rabbit”);
rabbit.__proto__.sayHi(); //
undefined