函数操作(this操作)

1.apply/call函数:会改变this关键字,并且第一个参数作为this关键字。

/*apply与call区别*/
console.log(Array.prototype.join.call(['Hello', 'world'], ' ')); //接受一列参数
console.log(Math.max.apply(null, [1,2,3])); //接受一个数组参数

  

2.bing:会改变this关键字,并且第一个参数作为this关键字。

function Dur() {
    this.time=1000
    this.getTime=function () {
        console.log(this.time)
    }
}
var dur = new Dur()
dur.getTime()  //this指向本身  1000
var newGetTime = dur.getTime.bind({time:200}) //改变getTime方法内的this,返回新方法
newGetTime()  //200

  

原文地址:https://www.cnblogs.com/jiebba/p/7601963.html