Learning JavaScript with MDN (call, apply, bind)

call, apply, bind

Object.prototype.toString()

检测 js 数据类型

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

Object.prototype.toString({})
// "[object Object]"
Object.prototype.toString([])
// "[object Object]"
Object.prototype.toString(Symbol())
// "[object Object]"
Object.prototype.toString(BigInt(1n))
// "[object Object]"

call, apply, bind

相同点: call, apply, bind 都是改变 this 的指向!

不同点:

  1. call 会立即执行, 参数列表(arg1, arg2, arg3, ...)

Object.prototype.toString.call([]);
// "[object Array]"
Object.prototype.toString.call(Symbol());
// "[object Symbol]"
Object.prototype.toString.call(BigInt(1n));
// "[object BigInt]"
  1. apply 会立即执行,参数数组 ([arg1, arg2, arg3, ...])

Object.prototype.toString.apply([]);
// "[object Array]"
Object.prototype.toString.apply(Symbol());
// "[object Symbol]"
Object.prototype.toString.apply(BigInt(1n));
// "[object BigInt]"
  1. bind 不会立即执行, 需要手动调用

Object.prototype.toString.bind([]);
// ƒ toString() { [native code] }
Object.prototype.toString.bind([])();
"[object Array]"
// "[object Array]"
Object.prototype.toString.bind(Symbol());
// ƒ toString() { [native code] }
Object.prototype.toString.bind(Symbol())();
// "[object Symbol]"
Object.prototype.toString.bind(BigInt(1n));
// ƒ toString() { [native code] }
Object.prototype.toString.bind(BigInt(1n))();
// "[object BigInt]"

refs

https://www.cnblogs.com/xgqfrms/p/9209381.html

https://www.cnblogs.com/xgqfrms/p/13019790.html

https://stackoverflow.com/questions/15455009/javascript-call-apply-vs-bind



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


Learning JavaScript with MDN (call, apply, bind)的更多相关文章

  1. JavaScript学习(2)call&apply&bind&eval用法

    javascript学习(2)call&apply&bind&eval用法 在javascript中存在这样几种特别有用的函数,能方便我们实现各种奇技淫巧.其中,call.bi ...

  2. Learning JavaScript with MDN & 使用 MDN 学习 JavaScript

    Learning JavaScript with MDN & 使用 MDN 学习 JavaScript Learn JavaScript with MDN 和 MDN 一起学习 JavaScr ...

  3. 别真以为JavaScript中func.call/apply/bind是万能的!

    自从学会call/apply/bind这三个方法后我就各种场合各种使用各种得心应手至今还没踩过什么坑,怎么用?说直白点就是我自己的对象没有某个方法但别人有,我就可以通过call/apply/bind去 ...

  4. 深入浅出:了解JavaScript中的call,apply,bind的差别

     在 javascript之 this 关键字详解文章中,谈及了如下内容,做一个简单的回顾:         1.this对象的涵义就是指向当前对象中的属性和方法.       2.this指向的可变 ...

  5. javascript中的call(),apply(),bind()方法的区别

    之前一直迷惑,记不住call(),apply(),bind()的区别.不知道如何使用,一直处于懵懂的状态.直到有一天面试被问到了这三个方法的区别,所以觉得很有必要总结一下. 如果有不全面的地方,后续再 ...

  6. 理解 JavaScript call()/apply()/bind()

    理解 JavaScript this 文章中已经比较全面的分析了 this 在 JavaScript 中的指向问题,用一句话来总结就是:this 的指向一定是在执行时决定的,指向被调用函数的对象.当然 ...

  7. Javascript中call,apply,bind方法的详解与总结

    在 javascript之 this 关键字详解 文章中,谈及了如下内容,做一个简单的回顾: 1.this对象的涵义就是指向当前对象中的属性和方法. 2.this指向的可变性.当在全局作用域时,thi ...

  8. javascript中call,apply,bind的用法对比分析

    这篇文章主要给大家对比分析了javascript中call,apply,bind三个函数的用法,非常的详细,这里推荐给小伙伴们.   关于call,apply,bind这三个函数的用法,是学习java ...

  9. Javascript中call,apply,bind的区别

    一.探索call方法原理 Function.prototype.call = function(obj) { // 1.让fn中的this指向obj // eval(this.toString().r ...

随机推荐

  1. Maven 依赖机制

    概述 在 Maven 依赖机制的帮助下自动下载所有必需的依赖库,并保持版本升级.让我们看一个案例研究,以了解它是如何工作的.假设你想使用 Log4j 作为项目的日志.这里你要做什么? 传统方式 访问 ...

  2. WPF学习里程(二) XAML基础

    1.什么是XAML? 官方语言: XAML是eXtensible Application Markup Language的英文缩写,相应的中文名称为可扩展应用程序标记语言,它是微软公司为构建应用程序用 ...

  3. nginx 配置文件解读

    参考:链接 在微服务的体系之下,Nginx正在被越来越多的项目采用作为网关来使用,配合 Lua 做限流.熔断等控制 --源自 nginx Lua 脚本语言,用标准C语言编写并以源代码形式开放, 其设计 ...

  4. go语言常见面试题

    前言 从网上找了一些面试题,觉得有意思的我都记录下来,自己学习和大家一起学习吧. 有些初级的题目只放答案,一些值得探讨的问题我会写上我自己的分析过程,希望大家多多交流. 原文链接 选择题 1.[初级] ...

  5. 深度学习论文翻译解析(十九):Searching for MobileNetV3

    论文标题:Searching for MobileNetV3 论文作者:Andrew Howard, Mark Sandler, Grace Chu, Liang-Chieh Chen, Bo Che ...

  6. The Department of Redundancy Department

    Write a program that will remove all duplicates from a sequence of integers and print the list of un ...

  7. 2019 徐州网络赛 M Longest subsequence t

    对于答案来说,一定是 前 i-1 个字符和 t的前 i 个一样,然后第 i 个字符比 t的 大 \(i\in [1,m]\) 前缀为t,然后长度比t长 对于第一种情况,枚举这个 i ,然后找最小的 p ...

  8. F - To Add Which?

    Description There is an integer sequence with N integers. You can use 1 unit of cost to increase any ...

  9. hdu2639 Bone Collector II

    Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in the ...

  10. Codeforces Round #656 (Div. 3) D. a-Good String (DFS)

    题意:有一个长度为\(n=2^k\)的字符串,对于某个字符\(c\),我们定义他是一个\(c-good\),如果: ​ 1.\(len=1\),并且\(s[1]=c\). ​ 2.\(len>1 ...