zoukankan      html  css  js  c++  java
  • js中call方法和apply方法解析

    今天小熙带大家来详细的了解一下js中的call和apply方法。这两个方法基本上是一个意思,区别在于 call 的第二个参数可以是任意类型,而apply的第二个参数必须是数组,也可以是arguments,还有 callee,caller..

    1、方法定义

    call方法: 
    语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 
    定义:调用一个对象的一个方法,以另一个对象替换当前对象。 
    说明: 
    call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。 
    如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。 

    apply方法: 
    语法:apply([thisObj[,argArray]]) 
    定义:应用某一对象的一个方法,用另一个对象替换当前对象。 
    说明: 
    如果 argArray 不是一个有效的数组或者不是 arguments 对象,那么将导致一个 TypeError。 
    如果没有提供 argArray 和 thisObj 任何一个参数,那么 Global 对象将被用作 thisObj, 并且无法被传递任何参数。

    2、常用实例

    Java代码  收藏代码
    1. function Animal(){    
    2.     this.name = "Animal";    
    3.     this.showName = function(){    
    4.         alert(this.name);    
    5.     }    
    6. }    
    7.   
    8. function Cat(){    
    9.     this.name = "Cat";    
    10. }    
    11.    
    12. var animal = new Animal();    
    13. var cat = new Cat();  
    14.  
    15. animal.showName();//弹出Animal
    16. animal.showName.call(cat,""); //自动执行弹出Cat

    17.     
    18. //通过call或apply方法,将原本属于Animal对象的showName()方法交给对象cat来使用了。    
    19. //输入结果为"Cat"    
    20. animal.showName.call(cat,",");    
    21. //animal.showName.apply(cat,[]);  

    /这个是说 cat 已经继承了animal的方法了,虽然cat木有showName这个方法,但是通过call,已经把参数都传给了cat,cat直接调用继承到的showName()方法即可  

    c、实现继承

    1. function Animal(name){      
    2.     this.name = name;      
    3.     this.showName = function(){      
    4.         alert(this.name);      
    5.     }      
    6. }      
    7.     
    8. function Cat(name){    
    9.     Animal.call(this, name);    
    10. }      
    11.     
    12. var cat = new Cat("Black Cat");     
    13. cat.showName();  //弹出Black Cat

    执行过程:

    首先执行var cat = new Cat("Black Cat");进入function Cat(name){  
         Animal.call(this, name);  
    }
    这时候的this为Cat{}对象,并非Animal,再接执行function Animal(name){    
         this.name = name;    
         this.showName = function(){    
             alert(this.name);    
         }    
    }
    此时的this对象绑定为Cat{},因此是Cat对象获得了两个属性为:Cat{name:"Black Cat",showName:function(){    
             alert(this.name);    
         }},回到var cat=Cat{name:"Black Cat",showName:function(){    
             alert(this.name);    
         }}

    最后才是cat.showName();

    d、多重继承

    1. function Class10()  
    2. {  
    3.     this.showSub = function(a,b)  
    4.     {  
    5.         alert(a-b);  
    6.     }  
    7. }  
    8.   
    9. function Class11()  
    10. {  
    11.     this.showAdd = function(a,b)  
    12.     {  
    13.         alert(a+b);  
    14.     }  
    15. }  
    16.   
    17. function Class2()  
    18. {  
    19.     Class10.call(this);  
    20.     Class11.call(this);  
    21. }  
    22. var aa = new Class2();  aa.showSub(7,2)//弹出5

     很简单,使用两个 call 就实现多重继承了。

    听完小熙的讲解和贴码,大家是不是对call和apply方法有所了解了呢~~如有不懂,欢迎大家留言哦

  • 相关阅读:
    struts2类型转换2
    list集合排序3
    list集合排序2
    list集合排序
    JSON
    Java获取文件Content-Type(Mime-Type)
    struts2文件下载
    获取文件大小
    struts2国际化
    人人都是产品经理
  • 原文地址:https://www.cnblogs.com/it-liuyuxi/p/6428617.html
Copyright © 2011-2022 走看看