类中,子类继承父类静态方法的原理

84 阅读1分钟
extendStatics=(function (Son,Parent) {
   function getStaticExtendsWithForIn(Son,Parent) {
        for (let key in Parent) {
            if(Object.prototype.hasOwnProperty.call(Parent,key)){
                Son[key]=Parent[key]
            }
            
        }
    }  
    function getStaticExtendsWithObjectkeys(Son,Parent) {
        Object.keys(Parent).forEach(key=>{
            Son[key]=Parent[key]
        })
    }
    function getStaticExtendsWithProto (Son,Parent) {
        Son.__proto__=Parent
    }
    return function (Son,Parent) {
        let MyextendStatics= Object.setPrototypeOf || getStaticExtendsWithForIn || getStaticExtendsWithObjectkeys || getStaticExtendsWithProto
        return MyextendStatics (Son,Parent)
    }
}())

function People (name, sex, phone) {//父类 【父构造函数】
    this.name = name;
    this.sex = sex;
    this.phone = phone;
  }
  
  People.count = 300;
  function ChinesePeople (name, sex, phone, national) {//ChinesePeople子类【子构造函数】
  
    People.call(this, name, sex, phone)
    this.national = national;//民族
  }
  extendStatics(ChinesePeople, People)
  console.log("ChinesePeople.count:", ChinesePeople.count)