Although  apply and  call  can implement same function. However, there is a litter different between them. Please pay attention to look at the examples below:

Define an object Person

 Person=function(name,age){
this.name=name;
this.age=age; } var pin=new Person("Pin",26); //create an object

After that, we need to define some functions (skating,running,eating) with different parameters respectively.

 function skating() // It don't need any parameter
{
alert(this.name+" like skating!");
} function running(distance) // It need 1 parameter
{
alert(this.name+" can only run "+distance+" meters!");
} function eating(fruit1,fruit2) // It need 2 paramters
{
alert(this.name+" like eating "+fruit1+" and "+fruit2+"!");
}

Now, I'll use apply and call respectively.

//after using apply
skating.apply(pin);
running.apply(pin,[1000]);
eating.apply(pin,["apple","orange"]); Results respectively: Pin like skating!
Pin can only run 1000 meters!
Pin like eating apply and orange!
//after using call
skating.call(pin);
running.call(pin,1000);
eating.call(pin,"apple","orange"); Results respectively: Pin like skating!
Pin can only run 1000 meters!
Pin like eating apply and orange!

From the results above, we can know the differences between apply and call :

Same:

1. apply and call all can make object pin implement sakting, running and eating, respectively. Exactly, object pin own three new functions (sakting, running and eating).

2. The first parameter pin is the replace the key this among three functions. So, when the three functions call the variable this.name, the key this is NOT three functions themselves but object pin.

3. If the first paramter is null, the global object window will replace it.

4. If the function (such as skating) don't need any paramters, the apply and call are same.

Difference:

If the function need a parameter at least, the parameter should be a array using apply. For the call, without any constraints.

How to use the functions of apply and call的更多相关文章

  1. Think Python - Chapter 03 - Functions

    3.1 Function callsIn the context of programming, a function is a named sequence of statements that p ...

  2. $q -- AngularJS中的服务(理解)

      描述 译者注: 看到了一篇非常好的文章,如果你有兴趣,可以查看: Promises与Javascript异步编程 , 里面对Promises规范和使用情景,好处讲的非常好透彻,个人觉得简单易懂. ...

  3. Qt Creator调试

    与调试器交互的几种方法: 1.单行运行或者单指令运行 2.中断程序运行 3.设置断点 4.检查调用栈空间的内容 5.检查并修改局部或者全局变量 6.检查并修改被调试程序的寄存器和内存内容 7.检查装载 ...

  4. $q -- AngularJS中的服务

    此 承诺/延迟(promise/deferred)实现 的灵感来自于 Kris Kowal's QCommonJS Promise建议文档 将承诺(promise) 作为和 异步执行操作(action ...

  5. modsecurity配置指令学习

    事务(transactions) Console(控制台) 1 Introduction Modsecurity是保护网络应用安全的工作.不,从零开始.我常称modsecurity为WAF(网络应用防 ...

  6. YUI之数组操作

    YUI的构建数组,将类数组转换成真正的数组,从而可以使用数组的所有方法   数组构建 //真正的数组返回1,类数组返回2,其余的返回0 YArray.test = function (obj) { v ...

  7. angular的$q服务和promise模式

    此承诺/延迟(promise/deferred)实现的灵感来自于 Kris Kowal's Q CommonJS Promise建议文档 将承诺(promise) 作为和 异步执行操作(action) ...

  8. UltraEdit配置python和lua环境

    [语法高亮] 在UltraEdit的wordfile中添加python和lua的语法支持(红色的为python,蓝色的为lua): /L10"Python" Line Commen ...

  9. swift闭包 notes http://www.gittielabs.com

    Swift Closureshtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror- ...

随机推荐

  1. iOS简历书写注意事项

    1.个人信息模块 1)简历标题 2)姓名 性别  年龄 电话  邮箱  常驻地 学历 英语能力 工作年限 籍贯 专业 (突出优势) 注意:不要从招聘网站导出简历网站 2.求职意向 1)职位 地点 薪资 ...

  2. LR脚本信息函数-lr_get_host_name

    lr_get_host_name() 返回主机的名称. char * lr_get_host_name(); lr_get_host_name函数返回执行脚本的机器的名称. 示例:lr_get_hos ...

  3. Java使用SQLServerBulKCopy实现批量插入SQLSqerver数据库

    这是CodingSir的帖子说的(由于不够详细,我现在提供给详细的,上手即用): Microsoft SQL Server 的bcp命令可以快速将大型文件复制插入到数据库中,C#提供了SqlBulkC ...

  4. Elasticsearch修改分词器以及自定义分词器

    Elasticsearch修改分词器以及自定义分词器 参考博客:https://blog.csdn.net/shuimofengyang/article/details/88973597

  5. SpringMVC 学习笔记(四)

    41. 尚硅谷_佟刚_SpringMVC_返回JSON.avi SpringMVC中使用@ResponseBody注解标注业务方法,将业务方法的返回值做成json输出给页面 导包: 除了一些sprin ...

  6. 你的 IDEA 是如何配置的?卡不卡?试试这样配置

    本文作者在和同事的一次讨论中发现,对 IntelliJ IDEA 内存采用不同的设置方案,会对 IDE 的速度和响应能力产生不同的影响. Don't be a Scrooge and give you ...

  7. Spring7——开发基于注解形式的spring

    开发基于注解形式的spring SpringIOC容器的2种形式: (1)xml配置文件:applicationContext.xml; 存bean:<bean> 取bean: Appli ...

  8. Cannot use 'in' operator to search for '23' in

    在用$.each(data,function(){i,n})时,有时会报错 Cannot use 'in' operator to search for '23' in,

  9. 基于4G Cat.1的内网穿透实例分享

    上一篇分享了:小熊派4G开发板初体验 这一篇继续BearPi-4G开发板实践:内网穿透实验. 基本TCP的socket通信测试 之前我们学习WiFi模块时,与PC进行TCP协议的socket通信测试我 ...

  10. 浏览器缓存_HTTP强缓存和协商缓存

    浏览器缓存 浏览器缓存是浏览器在本地磁盘对用户最近请求过的文档进行存储,当访问者再次访问同一页面时,浏览器就可以直接从本地磁盘加载文档. 所以根据上面的特点,浏览器缓存有下面的优点: 减少冗余的数据传 ...