Javascript - jshint Possible severe bug error

I am developing a client side export table plugin. The plugin works great. But when I checked my code in jshint, it gave me an error, talking about a possible serious violation. Below is the function:

function disableExport(){
        if(_(this).exportPlugin !== undefined){
            _(this).exportPlugin.setStyle('pointer-events', 'none');
            _(this).exportPlugin.find('.ebIcon').removeModifier('interactive','ebIcon');
            _(this).exportPlugin.find('.ebIcon').setModifier('disabled','','ebIcon');
        }else{
            console.log('ERROR!');
        }
    }

And he says: "If a strict mode function is executed by calling the function, its value 'this' will be undefined."

The full plugin code is available at https://jsfiddle.net/t47L8yyr/

How to resolve this? any other solution than using/*jshint validthis:true*/

+6
source share
1 answer

Inside your function, disableExport()you are referencing this. If you usually call a function ...

disableExport()

... strict Javascript, this undefined. this window. :

disableExport() // `this` will be `window`

"use strict"
disableExport() // `this` will be undefined

. window, :

_(window).exportPlugin(...) // reference `window`, not `this`

this , Function.call() Function.apply(), , this:

function disableExport(target) {
  if(_(target).exportPlugin !== undefined) {
    // ...
  }
}

disableExport(window) target. this , prototype ES6 class.

+5

All Articles