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. SQL Beautifier & SQL2014自带的格式化工具

    格式化工具(希望有几款集成在IDE中的格式化工具)为什么要说明这些,不是为说明这个工具而发,看到那几千行或集成在一起的存储过程觉得乱七八的不爽,后面将会强力训练下自己. --下面这款SQL Beaut ...

  2. (九)logback.xml文件配置(按时间、文件大小和log名称生成日志)

    原文链接:https://www.cnblogs.com/hhhshct/p/9084036.html rollingPolicy 设置:<rollingPolicy class="c ...

  3. 封装find_element

    因为find_element_by_id,find_element_by_name底层都是用find_element实现元素查找 #!/usr/bin/env python # -*- coding: ...

  4. tcpdump使用和抓包分析

    参考资料: http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html tcpdump可以将网络中传送的数据包的“头”完全截获下来 ...

  5. Quartz.Net系列(六):Quartz五大构件Trigger之TriggerBuilder解析

    所有方法图: 1.Create.Build Create:创建一个TriggerBuilder Build:生成Trigger var trigger = TriggerBuilder.Create( ...

  6. SSM-框架搭建-tank后台学习系统

    一.前言 最近收到很多网友给我私信,学习软件开发有点吃力,不知道从何处开始学习,会点基础但是做不出来什么项目, 都想放弃了.我就回复道:当下互联网飞速发展,软件开发行业非常吃香而且前景相当不错.希望能 ...

  7. ElasticSearch--validate验证搜索语句是否合法或者存在语法错误

    GET /accounts/person/_validate/query?explain { "query":{ "match": { "user&q ...

  8. maven测试配置是否成功

    测试maven 是否安装成功 win+r 输入 cmd 调出doc 输入 mvn -v maven安装完成测试报错 后来发现是java-home在path中配置缺少bin,如下图 这个是 java-h ...

  9. Jquery的一些方法

    $.trim(str);说明:去掉字符串首尾空格.

  10. 洛谷 P4822 [BJWC2012]冻结

    之前没学分层图,所以先咕了一下hiahiahia. 学完分层图了回来水写题解了. 这道题要用分层图来解.分层图就是在我们决策的时候,再建k层图,一共k+1层,层与层之间是有向边(这个很重要的),权值为 ...