javascript中的this

it2022-05-05  153

this

非严格模式:window、严格模式:undefined

this指向是调用此函数的主体

定义再对象中的方法,this调用主体就是这个对象

var point = {            x:0,            y:0,            moveTo: function(x,y) {                this.x = x;                this.y = y;​           }       }

this指向函数调用的主体

objA.fun.call(objB); 调用主体换成objB

call和apply


方法a.call(对象)

切换方法A的调用主体,this指向函数的调用主体

方法.call:要切换的调用对象

 

 

对象内部的函数,存在函数嵌套,内部函数的this指向window对象

函数嵌套,软绑定,用that中转一下

var point = {            x:0,            y:0,            moveTo: function(x,y) {                var that = this;                function moveTo(x) {                    console.log(this);                    that.x = x;​               }           }}

 

转载于:https://www.cnblogs.com/zhangchunqing/p/10748440.html


最新回复(0)