call apply bind 改变this指向

function fn() {
            console.log('函数fn');
            console.log(this);
        }
        var l = {
                name: 'lisa'
            }
            // fn()
        fn.call(l)

// call 可以调用函数 改变函数的this指向  可以实现继承
        function father(uname, age, sex) {
            this.uname = uname
            this.age = age
            this.sex = sex
        }

        function son(uname, age, sex) {
            father.call(this, uname, age, sex)
        }
        var son = new son('章三', 18, '男')
        console.log(son);

// apply 可以改变this指向 参数为数组(伪数组)
var p = {
            name: 'lisa'

猜你喜欢

转载自blog.csdn.net/weixin_58414196/article/details/130514272