图解call、apply、bind的异同及各种实战应用演示
一、图解call、apply、bind的异同
JavaScript中函数可以通过3种方法改变自己的this指向,它们是call、apply、bind。它们3个非常相似,但是也有区别。下面表格可以很直观看出三者的不同
| 方法 | 是否直接执行函数 | 传入的参数 | 调用方式 |
| call | 是 |
(context,arg1,arg2,arg3...) 第二个参数之后都是实参 |
function.call(context,arg1,arg2,arg3...) |
| apply | 是 |
(context,[arg1,arg2,arg3...]) 第二个参数必须是数组 |
function.apply(context,[arg1,arg2,arg3...]) |
| bind | 否 |
(context,arg1,arg2,arg3...) 第二个参数之后都是实参 |
var newFunction = function.bind(context); newFunction(arg1,arg2,arg3...) |
二、实例:
1、call
var a = {x: 1};
var b = function (y, z) {
console.log(this.x + y + z)
};
b.call(a, 3, 4);//此时b的this(即b执行时的上下文)被改变为a,因此this.x为1,第二个参数之后都是传给b实参。
2、apply
var a = {x: 1};
var b = function (arr) {
console.log(this.x + arr[0] + arr[1])
};
b.call(a, [3, 4]);//此时b的this(即b执行时的上下文)被改变为a,第二个参数是一个数组
3、bind
var a = {x: 1};
var b = function (y, z) {
console.log(this.x + y + z)
};
var newB = b.bind(a);//此时b的this(即b执行时的上下文)被改变为a,由此生成一个新函数,函数不会立即执行。
newB(3, 4);//函数此时才执行
三、常用场景
1、数组之间追加
var array1 = [12, "foo", {name: "Joe"}, -2458];
var array2 = ["Doe", 555, 100];
Array.prototype.push.apply(array1, array2);
/* array1 值变为 [12 , "foo" , {name:"Joe"} , -2458 , "Doe" , 555 , 100] */
2、获取数组中的最大值和最小值
var numbers = [5, 458, 120, -215];
var maxInNumbers = Math.max.apply(Math, numbers); //
3、验证是否是数组(前提是toString()方法没有被重写过)
function isArray(obj){
return Object.prototype.toString.call(obj) === '[object Array]';
}
4、类(伪)数组使用数组方法
var domNodes = Array.prototype.slice.call(document.getElementsByTagName("*"));
5、数字求和
function sum() {
var numberSum = Array.prototype.reduce.apply(arguments, [function (prev, next) {
return prev + next;
}]);
console.log(numberSum);
}
sum(1, 2, 3);
备注:以上1-4的使用场景来自,有兴趣的同学可以前往了解更多:https://github.com/chokcoco/apply-call-bind/tree/master
图解call、apply、bind的异同及各种实战应用演示的更多相关文章
- call、apply、bind的异同
一.call.apply.bind的异同 JavaScript中函数可以通过3种方法改变自己的this指向,它们是call.apply.bind.它们3个非常相似,但是也有区别.下面表格可以很直观看出 ...
- 学习前端的菜鸡对JS的call,apply,bind的通俗易懂理解
call,apply,bind 在JavaScript中,call.apply和bind是Function对象自带的三个方法,都是为了改变函数体内部 this 的指向. a ...
- call,apply,bind的用法
关于call,apply,bind这三个函数的用法,是学习javascript这门语言无法越过的知识点.下边我就来好好总结一下它们三者各自的用法,及常见的应用场景. 首先看call这个函数,可以理解成 ...
- JavaScript中call,apply,bind方法的总结。
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user:"追梦子", fn:f ...
- call(),apply(),bind()与回调
1.call(),apply(),bind()方法 JavaScript 中通过call或者apply用来代替另一个对象调用一个方法,将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定 ...
- JS 的 call apply bind 方法
js的call apply bind 方法都很常见,目的都是为了改变某个方法的执行环境(context) call call([thisObj[,arg1[, arg2[, [,.argN]]]] ...
- javascript-this,call,apply,bind简述2
上节我们一起研究了this这个小兄弟,得出一个结论,this指向调用this所在函数(或作用域)的那个对象或作用域.不太理解的朋友可以看看上节的内容,这次我们主要探讨一下call(),apply(), ...
- javascript-this,call,apply,bind简述1
最近在系统的学习面向对象方面的知识,遇到的最大拦路虎就数this的指向,call,apply,bind函数的使用,单独抽出一天时间把这几个烦人的家伙搞定,去学习更深入的内容. 首先介绍一下this的一 ...
- call,apply,bind方法的总结
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user:"追梦子", fn:f ...
随机推荐
- TestNg线程池配置、执行次数配置、超时配置
使用注解的方式对TestNg线程池配置.执行次数配置.超时配置 注:使用注解来控制测试方法运行的次数和超时时间,timeOut在单线程或者多线程模式下都可用,threadPoolSize设置了线程池的 ...
- sql server 2008 跨服务器查询
exec sp_addlinkedserver 'ITSV','','SQLOLEDB','192.168.202.53' exec sp_addlinkedsrvlogin 'ITSV','fals ...
- IComparer 指定排序。
public class NeEntityComparer : IComparer<NeEntity> { public int Compare(NeEntity x, NeEntity ...
- BestCoder Round #65
博弈 1002 ZYB's Game 题意:中文 分析:假定两个人是绝顶聪明的,一定会采取最优的策略.所以如果选择X的左边的一个点,那么后手应该选择X的右边对称的点,如果没有则必输,否则必胜,然后再分 ...
- 通过adb命令打印log
1.adb logcat --打印当前设备上所有日志 2.adb logcat | findstr *** --过滤仅含***的日志 3.adb logcat *:W --过滤打印严重级别W及以上 ...
- BZOJ 1053 & 反素数
题意: 反素数,膜一篇GOD's Blog...http://blog.csdn.net/ACdreamers/article/details/25049767 此文一出,无与争锋... CODE: ...
- Android 摇一摇 之 震动片
声明:import android.os.Vibrator; Vibrator mVibrator; 取得震动服务的句柄: mVibrator = (Vibrator)getApplication() ...
- Leetcode Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Leetcode Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Leetcode Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...