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- ...
随机推荐
- pip安装mysql-connector
pip install mysql-connector --index-url https://pypi.douban.com/simple
- Android学习笔记使用AlertDialog实现对话框
使用AlertDialog可以实现如下对话框 案例 布局问文件就加了几个Button,我直接上Java代码了 实现显示带取消,确定按钮的对话框按钮 Button showDialogOne = fin ...
- 《Java并发编程的艺术》 第9章 Java中的线程池
第9章 Java中的线程池 在开发过程中,合理地使用线程池能带来3个好处: 降低资源消耗.通过重复利用已创建的线程 降低线程创建和销毁造成的消耗. 提高响应速度.当任务到达时,任务可以不需要等到线程创 ...
- C# 什么是泛型 ?以及对泛型各方面的一些知识点的整理
1.1 理解什么是泛型 在.NET 2.0,可以成为革命性壮举的, 就是引入了激动人心的特性——泛型..NET泛型是CLR和高级语言共同支持的一种全新的结构,实现了一种将类型抽象化的通用处理方式.在泛 ...
- localStorage. sessionStorage、 Cookie不共同点:(面试题)
●存储大小的不同: localStorage的大小一般为5M sessionStorage的大小一般为5M cookies的大小一般为4K ●有效期不同: 1.localStorage的有效期为永久有 ...
- 微信小程序-返回并更新上一页面的数据
小程序开发过程中经常有这种需求,需要把当前页面数据传递给上一个页面,但是wx.navigateBack()无法传递数据. 一般的办法是把当前页面数据放入本地缓存,上一个页面再从缓存中取出. 除此之外还 ...
- MySQL LOAD DATA INFILE—批量从文件(csv、txt)导入数据
最近做的项目,有个需求(从Elastic Search取数据,业务运算后),每次要向MySQL插入1300万数据左右.最初用MySQL的executemany()一次插入10000条数据,统计的时间如 ...
- Linux Systemd 详细介绍: Unit、Unit File、Systemctl、Target
Systemd 简介 CentOS 7 使用 Systemd 替换了SysV Ubuntu 从 15.04 开始使用 Systemd Systemd 是 Linux 系统工具,用来启动守护进程,已成为 ...
- JavaWeb网上图书商城完整项目--day02-19.修改密码功能流程分析
我们来看看修改密码的业务流程操作:
- DNS信息收集-NSLOOKUP
上一篇文章简单介绍了nslookup的简单用法,这篇文章进一步介绍nslookup. nslookup可以指定DNS服务器,使用server参数: 可以看到,采用不同的DNS服务器的到的结果是不一样的 ...