JS中call()和apply()的用法

使用语法:

  • apply和call都能继承另外一个对象的属性和方法。
  • Function.apply(obj,args)方法能接受两个参数。
  • obj:这个对象将代替Function类里面的this对象。
  • args:这个数组将作为参数传递给Function。
  • call和apply的意识一样,只是参数列表不一样。
  • Function.call(obj,param1,param ,…)。
  • obj:这个对象将代替Function类里面的this对象。
  • param1:这是一个参数列表。

代码分析:

        function Person(name,age){
                this.name=name;
                this.age=age;
            }
            function Student(name,age,grade){
                //Person.apply(this,[name,age]);
                //Person.apply(this,arguments);
                Person.call(this,name,age);
                this.grade=grade;
            }
            var student1=new Student("Mike",12,"三年级");
            console.log("name:"+student1.name+"\n age:"+student1.age+"\n grade:"+student1.grade);

运行结果:
name:Mike
age:12
grade:三年级
分析:
Person.call(this,name,age);这里的this在创建对象的时候指代的是Student对象,也就是说用Student去执行Person类里面的内容,在Person类里面存在this.name等之类的语句,这样就将属性创建到了Student对象里面。

apply的一些其他用法:

  • apply可以将一个默认的数组转换为参数列表,列入,我们的[param1,param2,param3]转换为param1,param2,param3,这也就是为什么我们可以进行这样传递参数。
  • 用apply获取数组的求最大值最小值,Math.max(param)参数里面不支持数组。
            var b=(1,2,3);
            console.log(Math.max(b));//3
            var a=[1,2,34,21,4];
            var maxNum=Math.max.apply(null,a);
            console.log(maxNum);//34
            var minNum=Math.min.apply(null,a);
            console.log(minNum);//1
  • Array.prototype.push.apply可以实现两个数组的合并
            var arr1=[1,2,3,4];
            var arr2=[5,6,7,8];
            arr1.push(arr2);
            console.log(arr1);//[1,2,3,4,Array(4)]
            console.log(arr2);//[5,6,7,8]
            Array.prototype.push.apply(arr1,arr2);
            console.log(arr1); //[1,2,3,4,5,6,7,8]
            console.log(arr2);//[5,6,7,8]

分析:appl会将数组arr2隐式的转换为一个一个值,进行push。

call的一些其他用法:

  • 对象的替换,直接用A对象替换B对象
            function add(a,b){
                console.log(a+b);
            }
            function sub(a,b){
                console.log(a-b);
            }
            add.call(sub,3,1) //4 用add来替换sub
  • 直接用B对象来执行A对象中的方法
            function Class1(name){
                this.name=name;
                this.showName=function(){
                    console.log(this.name);
                }
            }           
            function Class2(name){
                this.name=name;
            }
            var c1=new Class1("mike");
            var c2=new Class2("lili");
            c1.showName.call(c2);//lili

分析:call的意识是把c1的方法放到c2上执行。

  • 使用call实现继承
            function Class1(){
                this.showText=function(text){
                    console.log(text);
                }
            }
            function Class2(){
                Class1.call(this);
            }
            var c2=new Class2();
            c2.showText("Hello!");//Hello!

分析:c2没有的属性和方法,通过call来继承。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值