A function's this argument is usually set implicitly, depending on how the function is called. Ordinary function calls, method calls, and constructor calls all lead to a different this value.

We can also call a function with an explicit this argument using Function.prototype.call() or Function.prototype.apply(). Both methods accept a thisArg as their first parameter and a variable number of additional arguments.

This lesson shows how to use call() and apply() and explains how they differ.

const numbers = [1, 2, 3, 4, 5];

// All the same
const slice1 = numbers.slice(1, 4);
const slice2 = numbers.slice.call(numbers, 1, 4);
const slice3 = numbers.slice.apply(numbers, [1, 4]);

Notice if you are not using "strict" mode, call() and apply() has some problem:

// If not using strict mode,  null and undefined value passed in .call() and .apply() will be ignored! this will be set to global. And this will not happen in strict mode

function foo() {
console.log(this == global)
} foo.call(null) // true
foo.call(undefined) // true foo.apply(null) // true
foo.apply(undefined) // true

[Javascript] Specify this using .call() or .apply()的更多相关文章

  1. 理解JavaScript中的arguments,callee,caller,apply

    arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[n] 参数function :选项.当前正在执行的 Function 对象的名字. n : ...

  2. 博文推荐】Javascript中bind、call、apply函数用法

    [博文推荐]Javascript中bind.call.apply函数用法 2015-03-02 09:22 菜鸟浮出水 51CTO博客 字号:T | T 最近一直在用 js 写游戏服务器,我也接触 j ...

  3. JavaScript学习笔记之call和apply

    前端的知识面太广了,想要记住所有知识点是不可能的,所以将这些学过的记录下来,随时都可以翻开来参考 1.call方法 调用一个对象的一个方法,call(this, arg1, arg2,argN);用来 ...

  4. JavaScript中callee与caller,apply与call解析

    1. arguments.callee 1.1 解释 返回正被执行的 Function 对象,也就是所指定的 Function 对象的正文. 1,.2 说明 callee 属性的初始值就是正被执行的 ...

  5. JavaScript中的bind,call和apply函数的用法和区别

    一直没怎么使用过JavaScript中的bind,call和apply, 今天看到一篇比较好的文章,觉得讲的比较透彻,所以记录和总结如下 首先要理解的第一个概念,JavaScript中函数调用的方式, ...

  6. JavaScript中bind、call、apply函数用法详解

    在给我们项目组的其他程序介绍 js 的时候,我准备了很多的内容,但看起来效果不大,果然光讲还是不行的,必须动手.前几天有人问我关于代码里 call() 函数的用法,我让他去看书,这里推荐用js 写服务 ...

  7. javascript中函数的call,apply及bind方法

    call 方法调用一个对象的一个方法,以另一个对象替换当前对象.call([thisObj[,arg1[, arg2[,  [,.argN]]]]])参数thisObj可选项.将被用作当前对象的对象. ...

  8. [转] 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)

    假如你是一个 JavaScript 开发者,你可能见到过 Array.prototype.slice.apply(arguments) 这样的用法,然后你会问,这么写是什么意思呢? 这个语法其实不难理 ...

  9. JavaScript中bind、call、apply函数使用方法具体解释

    在给我们项目组的其它程序介绍 js 的时候,我准备了非常多的内容,但看起来效果不大,果然光讲还是不行的,必须动手. 前几天有人问我关于代码里 call() 函数的使用方法.我让他去看书,这里推荐用js ...

  10. javascript之this、new、apply和call详解

    this指针的原理是个很复杂的问题,如果我们从javascript里this的实现机制来说明this,很多朋友可能会越来越糊涂,因此本篇打算换一个思路从应用的角度来讲解this指针,从这个角度理解th ...

随机推荐

  1. 自己封装js组件 - 中级中高级

    接着做关于alert组件的笔记 怎么又出来个中高级呢 对没错 就是出一个中高级来刷流量呵呵呵,但是中高级也不是白叫的 这次主要是增加了widget类,增加了自己绑定的事件和触发事件的方法!这么做是为什 ...

  2. sass09

    scss /* 1.使用自定义function和@each实现栅格布局. @function buildLayout($num: 5){ } 结果: col1{width: 20%} col2{wid ...

  3. hdu_2795,线段树,单点更新

    #include<iostream> #include<cstdio> #include<cstring> #define lson l,m,rt<<1 ...

  4. EL表达式获取参数值${param.name}等

    转自:http://www.html580.com/study/83.html EL表达式获取参数值${param.name}等 (1).${pageContext} 获取到 pageContext ...

  5. Find and counter

    Find: In a sense, find is the opposite of the [] operator. Instead of taking an index and extracting ...

  6. 15.map映射

    #include <iostream> #include <map> #include <cstring> using namespace std; //map常规 ...

  7. CUDA笔记(八)

    今天真正进入了攻坚期.不光是疲劳,主要是遇到的问题指数级上升,都是需要绕道的. 以visual profile来说,刚刚发现自己还没使用过. http://bbs.csdn.net/topics/39 ...

  8. 原生js实现发送验证码

    var form = { myfun:function(){ var el = form.config().el; var button = form.config().button; var tim ...

  9. UNIX 是什么?怎么诞生的?

    要记住, 当一扇门在你面前关闭的时候, 另一扇门就会打开. 肯·汤普森(Ken Thompson) 和丹尼斯·里奇(Dennis Richie)两个人就是这句名言很好的实例.他们俩是20世纪最优秀的信 ...

  10. NodeJS学习笔记 进阶 (7)express+session实现简易身份认证(ok)

    个人总结: 这篇文章讲解了express框架中如何使用session,主要用了express-session这个包.更多可以参考npm.js来看,读完这篇文章需要10分钟. 摘选自网络: 文档概览 本 ...