call apply bind 区别

apply call bind 是Function.prototype 下的方法,用于改变函数运行时的上下文,若没有返回值,则返回undefined;

使用apply() 可以继承其他对象的方法

注意:apply()第一个参数是null,在 非严格模式下,第一个参数为 null 或 undefined 时会自动替换为指向全局对象,

apply() 第二个参数为数组或类数组 

eg:
 参数二使用数组字面量
 function.apply(this,['eat','bananas'])

或使用数组对象
function.apply(this, new Array('eat','bananas') )
 
ES5开始,可以使用类数组

call() 方法

call() 是 apply()的一颗语法糖 作用和apply() 一样,

唯一的区别在于call() 接收的是参数列表,而apply()接收参数数组 +

 

bind() 方法

bind()的作用与call()和apply()一样,都是可以改变函数运行时上下文,

区别是 call() 和 apply 在调用函数后会立即执行, 而bind() 调用并改变函数运行时上下文后,返回一个新的函数,供需要时再调用。

eg:

原文地址:https://www.cnblogs.com/feijiediyimei/p/10938249.html