Native Prototypes Flashcards
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
The ________ method returns an array of a given object’s own property names, in the same order as we get with a normal loop.
The Object.keys() method returns an array of a given object’s own property names, in the same order as we get with a normal loop.
const object1 = { a: 'somestring', b: 42, c: false };
console.log(Object.keys(object1)); // expected output:
Array [“a”, “b”, “c”]
The _________method returns an array of a given object’s own enumerable property values, in the same order as that provided by a for…in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
The Object.values() method returns an array of a given object’s own enumerable property values, in the same order as that provided by a for…in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
const object1 = { a: 'somestring', b: 42, c: false };
console.log(Object.values(object1)); // expected output:
Array [“somestring”, 42, false]
The _________method returns an array of a given object’s own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for…in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
The Object.entries() method returns an array of a given object’s own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for…in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
const object1 = { a: 'somestring', b: 42 };
for (let [key, value] of Object.entries(object1)) {
console.log(${key}: ${value}
);
}
// "a: somestring" // "b: 42" // order is not guaranteed
The ___________ method returns an array of all symbol properties found directly upon a given object.
The Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly upon a given object.
const object1 = { a: 1, b: 2, c: 3 };
console.log(Object.getOwnPropertyNames(object1)); // expected output:
Array [“a”, “b”, “c”]
The ______________ method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.
The Object.getOwnPropertyNames() method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.
The static _____________method returns an array of the target object’s own property keys.
The static Reflect.ownKeys() method returns an array of the target object’s own property keys.
const object1 = {
property1: 42,
property2: 13
};
var array1 = [];
console.log(Reflect.ownKeys(object1)); // expected output:
Array [“property1”, “property2”]
const object1 = {
property1: 42,
property2: 13
};
var array1 = [];
console.log(Reflect.ownKeys(array1)); // expected output:
Array [“length”]
The __________ method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
const object1 = new Object(); object1.property1 = 42;
console.log(object1.hasOwnProperty('property1')); // expected output:
true
const object1 = new Object(); object1.property1 = 42;
console.log(object1.hasOwnProperty('toString')); // expected output:
false
const object1 = new Object(); object1.property1 = 42;
console.log(object1.hasOwnProperty('hasOwnProperty')); // expected output:
false