Object
Static Methods
create
- 创建一个空实例
- 空实例原型 -> proto
function create(proto) {
    let instance = {}
    Object.setPrototypeOf(instance, proto)
    return instance
}
 
Object.create = createassign
function assign(target, ...sources) {
    for (let source of sources) {
        for (let [key, val] of source) { // 自有属性
            target[key] = val
        }
    }
    return target
}
 
Object.assign = assignkeys
- 遍历对象的属性
- 仅保留对象的自有可枚举属性
function keys(obj) {
    let result = []
    for (let key in obj) {
        if (Object.hasOwn(obj, key)) {
            result.push(key)
        }
    }
    return result
}
 
Object.keys = keysvalues
- 遍历对象的属性
- 仅保留对象的自有可枚举属性
function values(obj) {
    let result = []
    for (let key in obj) {
        if (Object.hasOwn(obj, key)) {
            result.push(obj[key])
        }
    }
    return result
}
 
Object.values = valuesentries
- 遍历对象的属性
- 仅保留对象的自有可枚举属性
function entries(obj) {
    let result = []
    for (let key in obj) {
        if (Object.hasOwn(obj, key)) {
            result.push([key, obj[key]])
        }
    }
    return result
}
 
Object.entries = entriesOthers
instanceof
- 遍历 a的原型链
- 检查 b的原型是否在其中
function Instanceof(a, b) {
    let proto = Object.getPrototypeOf(a)
    while (proto) {
        if (b.prototype === proto) return true
        proto = Object.getPrototypeOf(proto)
    }
    return false
}new
- 创建一个空实例
- 调用构造函数(this-> 空实例)
- 空实例原型 -> 构造函数原型
function New(fn, ...args) {
    let instance = {}
    fn.apply(instance, args)
    Object.setPrototypeOf(instance, fn.prototype)
    return instance
}extends
- 子类中调用父类(this-> 子类this)
- 子类原型 -> 父类原型
// Parent
function Animal(type) {
    this.type = type
}
Animal.prototype.say = function() {
    console.log(this.type)
}
 
// Child
function Dog(name) {
    Animal.call(this, 'dog') // 继承实例方法
    this.name = name
}
Object.setPrototypeOf(Dog.prototype, Animal.prototype) // 继承原型方法
 
// instance of Child
let dog = new Dog('coco')