call
经常使用的场景,函数形参数组化,然后使用数组的一些方法
Array.property.slice.call(arguments)
function person() {console.log(this.name)}var egg = {name: "笑"}person.call(egg) // 笑
对于调用者person来说,call改变了this指向,this指向call的第一个参数: egg
对于egg来说,相当于是增加了person方法
即:
var egg = {name: '笑',person: function() {console.log(this.name)}}egg.person()

function person() {console.log(this.name)}var egg = {name: "笑"}Function.prototype.newCall = function(obj){console.log(this)};person.newCall(egg) // 输出person函数,因为person是调用者, this指向person
call()
call的第一个参数就是this指向
call除了第一个参数之外还可以接收多个参数,
apply除了第一个参数之外只能就收一个参数数组。
bind
bind会返回一个永久改变this指向的函数,不会立即执行,需要手动调用
bind().bind()链式调用的时候,this绑定第一个bind传入的对象
const name="lucy";const obj={name:"martin",say:function () {console.log(this.name);}};obj.say(); //martin,this指向obj对象setTimeout(obj.say,0); //lucy,this指向window对象// bindconst name="lucy";const obj={name:"martin",say:function () {console.log(this.name);}};const obj2={name:"xixixixi"}obj.say(); //martin,this指向obj对象setTimeout(obj.say.bind(obj).bind(obj2),0); //martin,this指向obj对象const name="lucy";const obj={name:"martin",say:function () {console.log(this.name);}};const obj2={name:"xixixixi"}obj.say(); //martin,this指向obj对象setTimeout(obj.say.bind(obj2).bind(obj),0); //xixixixi,this指向obj2对象obj.say.bind(obj).bind(obj2)()// martinobj.say.bind(obj2).bind(obj)()// xixixixi
