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. 带你学够浪:Go语言基础系列 - 8分钟学复合类型

    ★ 文章每周持续更新,原创不易,「三连」让更多人看到是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) " 对于一般的语言使用者来说 ,20% ...

  2. vulstack红队评估(五)

    一.环境搭建: ①根据作者公开的靶机信息整理 虚拟机密码: Win7: heart 123.com    #本地管理员用户 sun\Administrator dc123.com    #域管用户,改 ...

  3. excel如何快速汇总多个类别的总和?

    这个需求是一位在当前抗疫一线的朋友提出的,和各位分享一下. 需求情况 因为众所周知的原因,他每天都需要为照顾的小区居民购买.运送生活物资.小区居民通过表单的形式提交自己每日的需求,最终汇总到一张exc ...

  4. WeChair项目Beta冲刺(4/10)

    团队项目进行情况 1.昨日进展    Beta冲刺第四天 昨日进展: 前后端并行开发,项目按照计划有条不絮进行 2.今日安排 前端:扫码占座功能和预约功能并行开发 后端:扫码占座后端逻辑和预约功能逻辑 ...

  5. 手摸手带你理解Vue响应式原理

    前言 响应式原理作为 Vue 的核心,使用数据劫持实现数据驱动视图.在面试中是经常考查的知识点,也是面试加分项. 本文将会循序渐进的解析响应式原理的工作流程,主要以下面结构进行: 分析主要成员,了解它 ...

  6. Java-CORBA

    本文HelloCorba参考 Getting Started with JavaTM IDL 说在前面 Java TM IDL is a technology for distributed obje ...

  7. 3分钟理解NMS非极大值抑制

    1. NMS被广泛用到目标检测技术中,正如字面意思,抑制那些分数低的目标,使最终框的位置更准: 2. 假如图片上实际有10张人脸,但目标检测过程中,检测到有30个框的位置,并且模型都认为它们是人脸,造 ...

  8. 补充:回答网友的问题,如何不用路径,而直接将CImage画到DC中,之后DC一起显示.

    补充:回答网友的问题,如何不用路径,而直接将CImage画到DC中,之后DC一起显示.注释掉 pDC->BeginPath(); // 打开路径层 pDC->Rectangle(0,0,p ...

  9. Pytorch | 详解Pytorch科学计算包——Tensor

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Pytorch专题的第二篇,我们继续来了解一下Pytorch中Tensor的用法. 上一篇文章当中我们简单介绍了一下如何创建一个Ten ...

  10. html table表格斜线表头的实现方法总汇

    在html中给table加一个斜线的表头有时是很有必要的,但是到底该怎么实现这种效果呢?总结了以下几种方法: 1.UI背景图实现 直接去找公司的UI,让她做一张图片,作为背景图片放到这里,然后撑满就可 ...