function person() {} person.prototype = { attr: {age:18,sex:"girl"}, say: function() {console.log("My age is " + this.attr.age);console.log("I am a " + this.attr.sex); }} var Marry = new person();Marry.say(); // My age is 18// I am a girl 改变指向function person() {} person.prototype = { attr: {age:18,sex:"girl"}, say: function() {console.log("My age is " + this.attr.age);console.log("I am a " + this.attr.sex); }} xiaoming ={ attr : {age:20,sex:"boy"} }; var Marry = new person();Marry.say(); Marry.say.call(xiaoming);// My age is 18// I am a girl// My age is 20// I am a boy 共同之处fun.call(thisArg[, arg1[, arg2[, ...]]]) function f(x,y){ console.log(x+y);}f.call(null, 1, 1)//return 2 2、call:则是直接的参数列表,主要用在js对象各方法互相调用的时候,使当前this实例指针保持一致,或在特殊情况下需要改变this指针。如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。fun.apply(thisArg, [argsArray]) function f(x,y){ console.log(x+y);}f.call(null, [1,1])//return 2 apply和call功能一样,只是传入的参数列表形式不同,其中 thisArg 是你想指定的上下文,他可以是任何一个 JavaScript 对象(JavaScript 中一切皆对象),call 需要把参数按顺序传递进去,而 apply 则是把参数放在数组里。 var array1 = [12 , "foo" , {name:"Joe"} , -2458];var array2 = ["Doe" , 555 , 100];Array.prototype.push.apply(array1, array2);console.log(array1);//[12, "foo", Object, -2458, "Doe", 555, 100] 代码二var numbers = [5, 458 , 120 , -215 ];Math.max.apply(null,numbers);//458代码三
log(12 , "foo" , {name:"Joe"} , -2458);function log(){ var args = Array.prototype.slice.call(arguments); args.unshift("(app)"); console.log.apply(console, args);};// (app) 12 foo Object {name: "Joe"} -2458 bindfun.bind(thisArg[, arg1[, arg2[, ...]]])与上面不同的是,bind会返回一个改变this指向的新函数 ,注意这里强调的是新函数,其与之前的使用的不是同一块内存地址,所以当你需要重复使用这个函数的时候,你就不得不把其保存到一个变量,方便下次调用。上面的两个函数都是返回的执行结果,即调用即执
function f(x,y){ console.log(x+y);}f.call(null, [1,1])var new_f = f.bind(null,1,1)//return new functionnew_f(1)//return 2 需要说明的是,上面所有示例代码中的thisArg参数均用null来代替了,在未给出指定thisArg对象的情况下,null与undefined下this指向是全局对象,即js代码执行环境var obj = {bar: "Oops , this is a bad idea" }; var foo = {get: function() {return this.bar;}} var bind = foo.get.bind(obj) ,call = foo.get.call(obj) ,apply = foo.get.apply(obj); console.log(bind(),call,apply);console.log(bind,call,apply);console.log(typeof bind,typeof call,typeof apply);console.log(typeof bind(),typeof call,typeof apply); 
看到区别没有,区别是,当你希望改变上下文环境之后并非立即执行,而是回调执行的时候,使用 bind() 方法。而 apply/call 则会立即执行函数
apply 、 call 、bind 三者都是用来改变函数的this对象的指向的;apply 、 call 、bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文;apply 、 call 、bind 三者都可以利用后续参数传参;bind是返回对应函数,便于稍后调用;apply、call则是立即调用以上这篇深入理解JavaScript中的call、apply、bind方法的区别就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。