原型与原型链

基础
function Person(name, age) {
this.name = name;
this.age = age;
this.eat = function () {};
}
let p = new Person("Tom", 1);
- 构造函数 funciton Person()
- 实例 const person = new Person()
- 原型 Person.prototype
- 隐藏属性 constructor
person.constructor === PersonPerson.prototype.constructor === Personperson.__proto__ === Person.prototypePerson.__proto__ === Function.prototypeobj.__proto__ === Object.prototypefoo.__proto__ === Object.prototypeF.__proto__ === Function.prototypeF.__proto__.__proto__ === Object.prototype实例对象.xxx -> 构造函数.prototype.xxx -> Object.prototype.xxx
构造函数
当任意一个普通函数用于创建一个类对象时,它就被称作构造函数,或构造器。
- 默认函数首字母大写
- 通过 new 调用一个函数
- 构造函数返回的是一个对象
new
function myNew(constructor, ...args) {
if (typeof constructor !== 'function') {
throw 'constructor must be function'
}
const obj = Object.create(constructor.prototype)
const res = constructor.call(obj, ...args)
return typeof res === 'object' ? res : obj
}
instanceOf
function instanceOf(a, b) {
if (typeof a !== 'object' || b === null) {
return false
}
let photo = Object.getPrototypeOf(a)
const prototype = b.prototype
while (photo) {
if (photo === prototype) {
return true
}
photo = photo.__photo__;
}
return false;
}