this指向问题(易懂)

    技术2022-08-17  97

    1.一般函数中this指向window

    2.对象中函数的this指向,它的 this 是调用该函数的对象

    3.构造函数里的this,指向创建出来的实例

    4.事件处理函数中,它的this指向触发事件的元素

    #5.箭头函数没有自己的this,它的this指向上下文中的this(即所处环境的this)

    改变this指向
    function fn(x, y) { console.log('coming',this); console.log(x + y); } let obj={ name:'nick', hobby:'basketball' } fn.call(obj,5,7) fn.apply(obj,[5,7]) let emit = fn.bind(obj,5,7) emit()
    Processed: 0.020, SQL: 9