• 简单理解Javascript中的call 和 apply


    javascript中面向对像的能力是后来加进来的, 为了兼容性, 所以整出了很多奇特的东西, 

    function Animal(){    
        this.name = "Animal";    
        this.showName = function(){    
            alert(this.name);    
        }    
    }    
      
    function Cat(){    
        this.name = "Cat";    
    }    
       
    var animal = new Animal();    
    var cat = new Cat();    
        
    //通过call或apply方法,将原本属于Animal对象的showName()方法交给对象cat来使用了。    
    //输入结果为"Cat"    
    animal.showName.call(cat,",");    
    //animal.showName.apply(cat,[]);  

    所以,可以看出call和apply是为了动态改变this而出现的,当一个object没有某个方法,但是其他的有,我们可以借助call或apply用其它对象的方法来操作。


    用的比较多的,通过document.getElementsByTagName选择的dom 节点是一种类似array的array。它不能应用Array下的push,pop等方法。我们可以通过:
    var domNodes = Array.prototype.slice.call(document.getElementsByTagName("*"));
    这样domNodes就可以应用Array下的所有方法了。


  • 相关阅读:
    5-1
    浅谈sql中的in与not in,exists与not exists的区别
    理解SQL SERVER中的分区表
    SQLSERVER SQL性能优化
    SQL Server Profiler使用方法
    SQL Server中的三种Join方式
    执行计划
    执行计划sql
    INSERT INTO SELECT
    设计模式学习笔记-单例模式
  • 原文地址:https://www.cnblogs.com/answercard/p/6080065.html
Copyright © 2020-2023  润新知