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- ...
随机推荐
- (十)自动化测试pom完整文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 网页中为什么常用AT替换@(repost from https://zhidao.baidu.com/question/122291.html)
经常在个人主页上看到别人的邮箱地址中@被AT符号替代,很是迷惑,这样替代有什么好处呢?还是说html原有的原因使界面中不能出现@,查阅资料后解答如下: 写成AT [at],是为了防止被一些邮件扫描器搜 ...
- S7-200 PLC内部+5VDC电源的负载能力
S7-200 PLC内部+5VDC电源的负载能力 S7-200 CPU模块提供DC5V和24V电源:当有扩展模块时,CPU通过I/O总线为其提供5V电源,所有扩展模块的SV电源消耗之和不能超过该CPU ...
- Day7-微信小程序实战-天气预报小程序
前段时间在B站跟着一个视频,搞天气预报小程序 https://www.bilibili.com/video/BV1cJ411879s 但是因为这个调用的接口要money,太贵了就没买,就只是做了一些不 ...
- selenium(1)-详细解读元素定位的八种方式
安装selenium和下载webdriver 安装selenium pip install selenium pip install selenium -U (判断是否有最新版本) 下载drive ...
- Machine Learning Note
[Andrew Ng NIPS2016演讲]<Nuts and Bolts of Applying Deep Learning (Andrew Ng) 中文详解:https://mp.weixi ...
- Struts2 自定义拦截器时Action无法接收到参数
问题:自定义拦截器,没有添加defaultStack导致Action无法接受到参数 解决办法: 方法一,添加defaultStack,然后在Action中引用 自定义的stack,其实defaultS ...
- 入门大数据---HiveCLI和Beeline命令行的基本使用
一.Hive CLI 1.1 Help 使用 hive -H 或者 hive --help 命令可以查看所有命令的帮助,显示如下: usage: hive -d,--define <key=va ...
- webpack的入门实践,看这篇就够了
webpack的入门实践 我会将所有的读者概括为初学者,即使你可能有基础,学习本节之前我希望你具有一定的JavaScript和node基础 文中的 ... ...代表省略掉部分代码,和上面的代码相同 ...
- Linux下nginx反向代理服务器安装与配置实操
1.我们只要实现访问nginx服务器能跳转到不同的服务器即可,我本地测试是这样的, 在nginx服务器里面搭建了2个tomcat,2个tomcat端口分别是8080和8081,当我输入我nginx服务 ...