this.XX is not defined

情况

公司加班撸前端代码,发现有一个js内调取自身方法报错

this.XX is not defined
__draw: function(redraw,mapMarkersArray){
        let that = this;
        mapMarkersArray.forEach(function(val,index,arr) {
            if(val.type == 1)
            {
                if(redraw)
                {
                    that.removeCircles()
                }
            }
        }) 
    }  

原因

直接导致的原因是“forEach”方法,forEach方法第二个参数为指定对象,如果没有填写则默认指向window

解决

__draw: function(redraw,mapMarkersArray){
        let that = this;
        mapMarkersArray.forEach(function(val,index,arr) {
            if(val.type == 1)
            {
                if(redraw)
                {
                    that.removeCircles()
                }
            }
        },this) 
    }  

 

你可能感兴趣的:(this.XX is not defined)