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

1 Person=function(name,age){
2 this.name=name;
3 this.age=age;7 }
8
9 var pin=new Person("Pin",26); //create an object

After that, we need to define some functions (skating,running,eating)
with different parameters respectively.


 1 function skating() // It don‘t need any parameter
2 {
3 alert(this.name+" like skating!");
4 }
5
6 function running(distance) // It need 1 parameter
7 {
8 alert(this.name+" can only run "+distance+" meters!");
9 }
10
11 function eating(fruit1,fruit2) // It need 2 paramters
12 {
13 alert(this.name+" like eating "+fruit1+" and "+fruit2+"!");
14 }

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,布布扣,bubuko.com

时间: 06-06

How to use the functions of apply and call的相关文章

swift闭包 notes http://www.gittielabs.com

Swift Closureshtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; } .CodeMirror-lines { padding: 4px 0px; } .CodeMirror pre { padding: 0px 4px; } .CodeMirror-scrollbar-f

LiveScript 操作符

The LiveScript Book The LiveScript Book 操作符 数字 标准的数学操作符: 1.1 + 2 # => 32.3 - 4 # => -13.6 * 2 # => 124.8 / 4 # => 2 取模运算符: 1.-3 % 4 # => -32.-3 %% 4 # => 1 幂运算符是右结合的,比一元运算符优先级高.^是**的语法糖: 1.2 ** 4 # => 162.3 ^ 4 # => 813.-2 ^ 2 ^ 3

$q -- AngularJS中的服务(理解)

描述 译者注: 看到了一篇非常好的文章,如果你有兴趣,可以查看: Promises与Javascript异步编程 , 里面对Promises规范和使用情景,好处讲的非常好透彻,个人觉得简单易懂. 既然是用来处理异步编程的,那么在浏览器端的JS里,主要是2种: setTimeout 和 Ajax 请求.  promise 的使用就很像Ajax请求的成功和失败回调. 此承诺/延迟(promise/deferred)实现的灵感来自于  Kris Kowal's Q CommonJS Promise建议

javascript多投事件的处理 (转)

出处 http://blog.csdn.net/dead_of_winter/article/details/1646367 尽管ecma标准指定了addEventListener这样的方法来实现事件多投机制,但是ie ns等浏览器却各行其道,这里利用语言本身的特性来实现事件多投,只要支持简单事件模型,就可以使用.同时FunctionArray也是一个独特的结 构,javascript有趣的特性的体现. <img id=img1 style="height:100;width:100;&q

modsecurity配置指令学习

事务(transactions) Console(控制台) 1 Introduction Modsecurity是保护网络应用安全的工作.不,从零开始.我常称modsecurity为WAF(网络应用防火墙),这是种被广泛接受的叫法,它指的是为保护网络应用而专门设计的产品族.也有些时候我称它为HTTP入侵检测工具,我认为这个称呼更好的描述了modsecurity做了什么. Understanding ModSecurity 像Apache为其他模块所做的一样,Apache为modsecurity处

Qt Creator调试

与调试器交互的几种方法: 1.单行运行或者单指令运行 2.中断程序运行 3.设置断点 4.检查调用栈空间的内容 5.检查并修改局部或者全局变量 6.检查并修改被调试程序的寄存器和内存内容 7.检查装载的共享库列表 8.反汇编代码段 9.创建当前被调试程序状态快照并在之后重新检测 调试器的使用 在调试模式中,可以选择Window->Views,打开需要的视窗查看相应的数据.默认情况下,视窗都是锁定在workspace中的,可以选择Window->Views->Locked取消锁定,视窗支持

DPDK QoS_meter 源码阅读

main.c /* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2010-2016 Intel Corporation */ #include <stdio.h> #include <getopt.h> #include <rte_common.h> #include <rte_eal.h> #include <rte_malloc.h> #include <rte_mempoo

Use apply to Call Functions with Different

Item 21: Use apply to Call Functions with DifferentNumbers of ArgumentsImagine that someone provides us with a function that calculates theaverage of any number of values: average(1, 2, 3); // 2 average(1); // 1 average(3, 1, 4, 1, 5, 9, 2, 6, 5); //

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio