构造函数绑定

117 阅读1分钟
!-- 
    构造函数绑定
在子类型构造函数的内部调用父类型构造函数
通过call()或apply()方法 或者bind()
 -->
<script>
    function Person() {
        this.foot = 2;
        this.head = 1;
    }
    // Person.prototype.say = function(){
    //     document.write(this.foot + this.head);
    // }
    function Student(name) {
        /* 使用call来改变Person的this,父构造函数中的this由Person改成Student */
        // Person.call(this);
        // Person.apply(this);
        Person.bind(this)();
        this.name = name;
    }
    let stu1 = new Student('lisi');
    // console.log(stu1);
    // document.write(stu1.foot);
    // stu1.say();
</script>