JSLint: used out of range for a triple set of variables

I have a code block like this:

/*global MYAPP: true*/
var MYAPP = MYAPP || {};

JSLint highlights "MYAPP" after the equal sign with the message "MYAPP used out of scope."

What is wrong with this?

+4
source share
2 answers

If you use var, you are declaring a local variable. If you execute MYAPP || {}, you usually try to set the default value for a global variable or for a variable declared earlier.

What should be the area MYAPP? If it’s global, then something like

MYAPP = window.MYAPP || {}; 

window. stops him from complaining that it is undefined

, ,

MYAPP = MYAPP || {};

, , then

var MYAPP = {};

, script (.. ), . script ( javascript ) , . script ( javascript), IIFE ( ). .

(function() {
    var MYAPP = {};
    //code that uses MYAPP;
})();
//MYAPP is not visible out here, or in other script tags/files.
+2

jshint, jslint, sonarqube .. var (let const).

, , , window , , , !

, . , ( JSDoc , , ).

/**
 * @fileOverview Starting point for Front-end [Your Project Name] JavaScript.
 * @author {@link http://www.lesieur.name/|Bruno Lesieur}
 * @version 1.0.0
 * @module Common
 * @requires {@link external:jQuery}
 */

var window = this,

    /**
     * Container for all function of website.
     * @namespace website
     * @global
     * @type {Object}
     */
    website = window.website || {},

    /**
     * Write less, Do more.
     * @external jQuery
     * @global
     * @see {@link https://jquery.com/|jQuery}
     */
    $ = window.$,

    /**
     * Shortcut for $(window).
     * @global
     * @type {jQuery}
     */
    $window = window.$window || $(window);

/**
 * All components (modules) avaiable on all pages.
 * @namespace components
 * @alias components
 * @type {Object}
 * @memberOf website.
 */
website.components = website.components || {};

, :

var window = this,
    MYAPP = window.MYAPP || {};

var global = this,
    MYAPP = global.MYAPP || {};

var MYAPP = this.MYAPP || {};
+1

All Articles