手写call方法

 Function.prototype.myCall=function (context,args) {
        console.log(arguments)
        //context 表示call里面的第一个参数也就是需要改变this指向的那个对象。
        //this表示这个方法
        //把这个方法挂到需要改变指向的对象身上调用,相当于把this指向了这个对象身上,从而达到改变this的作用。
        //argument是一个特殊的对象,可以获取方法传入的参数。
        context.fn=this
        let arr = []
        for(let i =1;i<arguments.length;i++){
            console.log(arguments[i])
            
                //arr.push(arguments[i])
                arr.push('arguments[' + i + ']');
           
        }
        console.log(arr)
        console.log(arguments[1])
        eval('context.fn('+arr+')')
        //context.fn();
        delete context.fn

    }


    let person = {
        name:'tom',
        age:22,
        sex:'male',
        sayName:function(){
            console.log(this.name)
        },
        showInfo:function(name,age,sex){
            this.name=name;
            this.age=age;
            this.sex=sex;
            console.log('name:'+this.name+',age:'+this.age+',sex:'+this.sex)
        }
    }

    let person2={
        name:'tony'
    }
    person.showInfo('lily',22,'female')
    person.showInfo.myCall(person2,'john',11,'male') //name:john,age:11,sex:male

文章来源地址https://uudwc.com/A/Pm32z

原文地址:https://blog.csdn.net/Mahuicool/article/details/133227290

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

h
上一篇 2023年09月24日 07:19
Qt5开发及实例V2.0-第二十章-Qt.QML动画特效
下一篇 2023年09月24日 07:19