简体   繁体   中英

What does the JSHint option “undef” do?

From reading the documentation the setting undef seems to be what controls the "x is not defined" warnings. But setting it to false does not stop those warnings. So this leaves me wondering what undef actually does.

Could someone please explain this better than the documentation does?

Note: To ignore these warnings I had to use /*jshint -W117 */

The undef option, when enabled, warns whenever non-local (neither a parameter nor "var" variable in scope) variable usage is found.

Taking the code in the example , the following results in 'myvar' is not defined. (This warning indicates that the code may result in a ReferenceError when the code is run; not that the value is "undefined".)

/*jshint undef:true */
function test() {
  var myVar = 'Hello, World';
  console.log(myvar); // Oops, typoed here. JSHint with undef will complain
}

When disabling the option there is no warning as it's assumed that the intent was to access the myvar gobal variable; this can in turn be accepted/validated with the global directive , and the following is once again warning-free.

/*jshint undef:true */
/*global myvar*/
function test() {
  var myVar = 'Hello, World';
  console.log(myvar); // Yup, we wanted the global anyway!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM