How to use the functions of apply and call
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的更多相关文章
- Think Python - Chapter 03 - Functions
3.1 Function callsIn the context of programming, a function is a named sequence of statements that p ...
- $q -- AngularJS中的服务(理解)
描述 译者注: 看到了一篇非常好的文章,如果你有兴趣,可以查看: Promises与Javascript异步编程 , 里面对Promises规范和使用情景,好处讲的非常好透彻,个人觉得简单易懂. ...
- Qt Creator调试
与调试器交互的几种方法: 1.单行运行或者单指令运行 2.中断程序运行 3.设置断点 4.检查调用栈空间的内容 5.检查并修改局部或者全局变量 6.检查并修改被调试程序的寄存器和内存内容 7.检查装载 ...
- $q -- AngularJS中的服务
此 承诺/延迟(promise/deferred)实现 的灵感来自于 Kris Kowal's QCommonJS Promise建议文档 将承诺(promise) 作为和 异步执行操作(action ...
- modsecurity配置指令学习
事务(transactions) Console(控制台) 1 Introduction Modsecurity是保护网络应用安全的工作.不,从零开始.我常称modsecurity为WAF(网络应用防 ...
- YUI之数组操作
YUI的构建数组,将类数组转换成真正的数组,从而可以使用数组的所有方法 数组构建 //真正的数组返回1,类数组返回2,其余的返回0 YArray.test = function (obj) { v ...
- angular的$q服务和promise模式
此承诺/延迟(promise/deferred)实现的灵感来自于 Kris Kowal's Q CommonJS Promise建议文档 将承诺(promise) 作为和 异步执行操作(action) ...
- UltraEdit配置python和lua环境
[语法高亮] 在UltraEdit的wordfile中添加python和lua的语法支持(红色的为python,蓝色的为lua): /L10"Python" Line Commen ...
- swift闭包 notes http://www.gittielabs.com
Swift Closureshtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror- ...
随机推荐
- 在Centos7中,从主机 Windows 上无法远程访问 Linux 上rabbitmq的解决方法
当在 Linux 上配置好 Rabbitmq服务器后,如果从主机中无法访问到 Linux 中的Rabbitmq服务器时,需要做如下的检查: 1. Rabbitmq是否启动成功 在控制台输入: ps - ...
- C# 9.0 新特性之模式匹配简化
阅读本文大概需要 2 分钟. 记得在 MS Build 2020 大会上,C# 语言开发项目经理 Mads Torgersen 宣称 C# 9.0 将会随着 .NET 5 在今年 11 月份正式发布. ...
- python下载及安装步骤
Python安装 1.浏览器打开网址:www.python.org 2.根据电脑系统选择下载 3.确定电脑系统属性,此处我们以win10的64位操作系统为例 4.安装python 3.6.3 双击下载 ...
- Springboot打包放到Tomcat中报错 One or more listener fail to start
1.问题: Springboot项目直接启动不报错,打war包放到外部容器Tomcat.东方通上,在@Weblistener注解的监听器类中报错 One or more listener fail t ...
- 用VC++6.0,双击主对话框中的按钮时,不能跳转到代码处
1. 首先在项目中--[生成]build--[清除解决方案]clean 2. 关闭项目 3. 删除项目中的[Debug]下所有文件 4. 把*.aps,*.clw,*.ncb,*.opt删掉----- ...
- vc++如何知道cppdlg所关联的对话框?
vc++ 6.0如何知道cppdlg所关联的对话框? 找a.cpp对应的a.h头文件里面找. https://blog.csdn.net/txwtech/article/details/1020824 ...
- windows下使用虚拟机安装linux操作系统
前言:虚拟机是开发者的好帮手,它可以帮助我们在同一台电脑上创建不同的环境,这样你就可以在不影响原有的环境下,使用另外一套新的环境去完成你的开发工作.相信不少在windows下开发的同学对此深有体会,本 ...
- Beta冲刺总结随笔
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 团队名称 WeChair 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺 作业正文 如下 其他参考文献 项目预期 ...
- SpringCloud教程第4篇:Hystrix(F版本)
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...
- easymock案例2
public interface IStudent { public String doMethod1(); public String doMethod2(); public String doMe ...