Code Example 1: Ways to work with prototype
❓ Какие способы получения и установки прототипа объекта существуют в JavaScript?
var parent = { x: 1 };
var child = Object.create(parent);
// Получение прототипа
Object.getPrototypeOf(child);
// Установка прототипа
Object.setPrototypeOf(child, newParent);
// Нестандартный способ
child.__proto__;
child.__proto__ = newParent;
Code Example 2: Object.getPrototypeOf()
❓ Что вернёт Object.getPrototypeOf(rabbit)? Чему равен Object.getPrototypeOf(Object.prototype)?
var animal = {
eats: true
};
var rabbit = Object.create(animal);
var proto = Object.getPrototypeOf(rabbit);
console.log(proto === animal);
console.log(proto.eats);
console.log(Object.getPrototypeOf(animal) === Object.prototype);
console.log(Object.getPrototypeOf(Object.prototype) === null);
Code Example 3: isPrototypeOf()
❓ Что выведут все console.log? Проверяет ли isPrototypeOf всю цепочку прототипов?
var animal = { eats: true };
var rabbit = Object.create(animal);
var whiteRabbit = Object.create(rabbit);
console.log(animal.isPrototypeOf(rabbit));
console.log(animal.isPrototypeOf(whiteRabbit));
console.log(rabbit.isPrototypeOf(whiteRabbit));
console.log(Object.prototype.isPrototypeOf(whiteRabbit));
Code Example 4: Using __proto__
❓ Что выведет rabbit.eats после установки rabbit.__proto__ = animal?
var animal = { eats: true };
var rabbit = { jumps: true };
console.log(rabbit.__proto__ === Object.prototype);
rabbit.__proto__ = animal;
console.log(rabbit.eats);
Code Example 5: getPrototypeOf vs __proto__ vs isPrototypeOf
❓ Чем отличаются три способа работы с прототипом? Какой из них стандартный в ES5?
getPrototypeOf:
var obj = { x: 1 };
var child = Object.create(obj);
var proto = Object.getPrototypeOf(child);
console.log(proto === obj);
console.log(Object.getPrototypeOf([]) === Array.prototype);
console.log(Object.getPrototypeOf(function(){}) === Function.prototype);
__proto__:
var obj = { x: 1 };
var child = Object.create(obj);
console.log(child.__proto__ === obj);
child.__proto__ = { y: 2 };
console.log(child.y);
console.log(child.x);
isPrototypeOf:
var animal = { eats: true };
var rabbit = Object.create(animal);
console.log(animal.isPrototypeOf(rabbit));
console.log(Object.prototype.isPrototypeOf(rabbit));
console.log(Object.prototype.isPrototypeOf(animal));
Code Example 6: Prototype of primitives
❓ Как получить прототип примитивного значения? Что произойдёт при прямом вызове Object.getPrototypeOf на примитиве?
console.log(Object.getPrototypeOf(Object("строка")) === String.prototype);
console.log(Object.getPrototypeOf(Object(42)) === Number.prototype);
Code Example 7: Object.create(null) prototype
❓ Что вернёт Object.getPrototypeOf(noProto)? Почему noProto.__proto__ возвращает undefined?
var noProto = Object.create(null);
console.log(Object.getPrototypeOf(noProto));
console.log(noProto.__proto__);
Code Example 8: isPrototypeOf vs instanceof
❓ В чём разница между isPrototypeOf и instanceof? Почему obj instanceof proto вызовет ошибку?
function Animal() {}
var rabbit = new Animal();
console.log(rabbit instanceof Animal);
console.log(Animal.prototype.isPrototypeOf(rabbit));
var proto = { x: 1 };
var obj = Object.create(proto);
console.log(proto.isPrototypeOf(obj));
Code Example 9: Getting the entire prototype chain
❓ Что вернёт getPrototypeChain для массива [1, 2, 3]? А для экземпляра new MyClass()?
function getPrototypeChain(obj) {
var chain = [];
var current = Object.getPrototypeOf(obj);
while (current !== null) {
chain.push(current);
current = Object.getPrototypeOf(current);
}
return chain;
}
var arr = [1, 2, 3];
console.log(getPrototypeChain(arr));
function MyClass() {}
var instance = new MyClass();
console.log(getPrototypeChain(instance));