“alert” is not defined when running www.jshint.com

后端 未结 8 1847
长发绾君心
长发绾君心 2020-12-15 02:46

I fixed this by simply adding var alert; However, is this what I should be doing to get the pesky error message to go away? Here is the fix. Here is the fail

相关标签:
8条回答
  • 2020-12-15 03:00

    Instead of

    alert('message')
    

    you should use

    window.alert('message');
    

    Because this method is defined in window object.

    This of course assumes you have browser option set to true in your .jshintrc, so this way jshint will know window object is exposed.

    "browser"       : true,     // Standard browser globals e.g. window, document.
    

    *The same thing happens with confirm().

    0 讨论(0)
  • 2020-12-15 03:02

    try passing window object in :

    (function (global) {
    
    "use strict";
    
    var alert;  //  added this in to fix
    
    function initialize_page()
      {
      global.alert ("hi");
      }
    
    addEventListener('load', initialize_page);
    
    })(window);
    
    0 讨论(0)
  • 2020-12-15 03:04

    Instead of:

    alert('message')
    

    I use:

    var alert = window['alert'];
    
    0 讨论(0)
  • 2020-12-15 03:05
    function prod(arr) {
        let largest = 0;
        let secondLargest = 0;
        const len = arr.length;
        for (let i = 0; i < len; i++) {
            if (arr[i] > largest) {
                secondLargest = largest;
                largest = arr[i];
            }
            else if (arr[i] < largest && arr[i] > second_largest) {
                secondLargest = arr[i]
            }
        }
        return largest * secondLargest;
    }
    
    console.log(prod([2, 4, 7, 8]));
    
    0 讨论(0)
  • 2020-12-15 03:09

    Use .jshintrc file or CTRL + , in VS Code, to edit options for jshint.

    in js alert(data.status); or window.alert(data.status);

    "window": true,
    "alert": true
    

    or best

    "devel": true,
    
    {
    "esversion": 6,
    "browser": true,
    "undef": true,
    "varstmt": true,
    "forin": true,
    "unused": true,
    "funcscope": true,
    "lastsemic": true,
    "moz": true,
    "jquery": true,
    "module": true,
    "devel": true,
    "globals": {
        "window": true,
        "document": true,
        "console": true,
        "alert": true
    }
    

    }

    0 讨论(0)
  • 2020-12-15 03:11

    declare alert as variable and it will work without any settings:

    for example:

    var alert;
    
    alert('hello world');
    
    0 讨论(0)
提交回复
热议问题