no-implicit-globals

禁止在全局范围内声明

最好的做法是避免 'polluting' 全局范围内的变量是脚本本地的。

从脚本创建的全局变量可能会与从另一个脚本创建的全局变量产生名称冲突,这通常会导致运行时错误或意外行为。

此规则不允许以下内容:

  • 在全局范围内创建一个或多个变量的声明。
  • 全局变量泄漏。
  • 只读全局变量的重新声明和对只读全局变量的赋值。

kOOy44oPZi4I52yYbISVDmv89YggOlszfxGBRUetf1lGmjeeznNEcf5wO4rxmVFPx0HTM7iLZMYVVxRqpBX/BZ5TS2lOFyetml4xOxGtaBkmSmEZM7J+C+6lBS9TI/gZA1czlCCoIQs4AZG/4MLaJw==

kn3JuYounIgLKiv2gmrGvHwxT9EQIPDTSoKhpkP+NnA3P7pMiXHfdo9qrvrhBHkmrRlyFTMvDdP+heXc2Bfq+k7cRnCyd9GLQvK6Cd4QfRpESUzUx+MPspojSKLrA0xt5lPfXes/+wvFQjrbfzrZClkNJrfNrnSCE0qPlG5UWQRnuglRJ336EagQAWRC7kqsaiwtqGBgIrKAoYwJ/pZFmR1YqBpYGWJGA1lqBbRlBSswMJLc2TlE3I0GjfcCDj/2A4lD6CTF4oJDatl/5haQ8g==

uItwcUBe+rKTbIYsS9Ga4hfH5ExjUz1tyYUIg42MZminhPWhDGZBjZpXd2Vn1EacR1EQRalMbkjj/fqqyt7loM0Gn+t/ZsK9pHv5HRSkJyBPxVWapQpWKRQ7EKT8K/3QKovHe/JETNLvnpUX4Wbgjw==

p1sIsUzWj7eKM+EUv2HNliLpRPMQq5aoCYKCuIkAUV5pynEWdzIKw7uzSU5YUtA/XIr/byDEyOowwh412SqnLg==

    GJMm0gGlISCe7VbzXNaqwX8rRd69fIQL3oP7464Kn+v5sPtk73gzgdloHx6b2cpGgms/LnKRcVXpLHoHcQB6KsczsOUwwIwBNKLibxKcKoc1P//y5Q7X7DD0DDiYT0OWpGLG3RSOd5DC2WTEhc2/vbzdiXxoAF0o8q1jIEVHnbh9A8t4b+9ICsETkAiRN5GCBH+j6Y7cZRpB/VGrF6+B0TjtOEPITV0P1NU9IahYS/qxy+8MRPGiBlkBCF2o61/m

规则详情

var 和 function 声明

emQ2HDTritVJRBBZX+KlcynA+RqPj0DSUU0eK3FVcgOZy9ruXdlU7iSw0g2uXiU/le3cgB/akwQJANPjMhYnwJgSseddLymitkjpDN7NasF2VruO569/tgOIcv/ZkyrnXuCcA+n9U9KuWkLF0Ii5mzqsCA9zo87Q8Gs7G/dRinRmOOWUyw6aPp18hjyK8Iq5tpA/5KQHvGal99LGLytzkfVM5Nhle97NhSvK2EJ9wyURtIP49RVTj0VuXjPWuvPz8dK5v2WG3riNEpaynH8o/uba2Cc0vyFqorR5uCrOQKxIXoovl674AX7UfJhXAoKljqx9rHgtV23msHMRkUhknm7PHDLHFqhid+L68oIsEGDvp7Y9OcMpJJlJqj3+BLIT7DXllYdbQI7XXnazJ3aoZ/ujK/LrNtFvOBNpFoAkN9/CP7BVCe/fBcFZWUk78HeR0197Bpf4LNAkU3dWLMqzG4vO4wWZ9UEDLohraA77tEE=

eidwC2N3vBzz/qmZ9oUbA+mrooboLDW0IkNYs7/cfraz7IQpMgG4xgueIQCwQ2lEXVvE75ab9zcRN2l/VIe2FIbuAUircxPWUwoDOaiIgSGoBt5pXtRJrm6W/R1Ti0IxJc2Bz4AM3hLsfyyO8R2xO3vo8jHezzy3mpJrR/qRUhLuPcxkYlhGpg5yECBKvcCfJ6q903QzunqiEdX8mfZiIrsHidgce8gwMdoZZ46A/aQ=

BBJWkULQKXOKIgBNo1Kf75ZIz4Bid3wdSxQL2AwoPrKMXjKPkT5hdLtnrWwkCnu5

/*eslint no-implicit-globals: "error"*/

var foo = 1;

function bar() {}

PwR5DrJrN80min96hKNCvTqBajX6zEp21bd6ZYasO8zgB97mz8MBbdVstv/4Rxmy

/*eslint no-implicit-globals: "error"*/

// explicitly set on window
window.foo = 1;
window.bar = function() {};

// intended to be scope to this file
(function() {
  var foo = 1;

  function bar() {}
})();

NQouUsAo3dzDe0feIcaILke8pF57YbS2Z6VeSAXVIUPg8eID8yoasggHwt3kH3EA+xnGCg9s4lNTty5f9BiOEBL4Xe3GW3L8BMeg8yv9W4UtSg5Iim1EFJpD+cTSAga6Nt4/2DipVhlCWlopsUvcnwsWaKhD5B+GCei8N84gRxI=

/*eslint no-implicit-globals: "error"*/

// foo and bar are local to module
var foo = 1;
function bar() {}

全局变量泄漏

G+8fLabm8Pk9Z4LdGlRMjC8u9CYz5x6qMIwMwv/q0RzPGF1pjgenCxEA7Ca4iGt3ull11eA9WAk6ZdNPu4r3jyOwdsA1uaZlMG2bbx/neNG807Yw7kGPrptsRvwTKR32UmT5oMAkO6ZRoQcXHcEXBEFLX8BvBL4G0Xrrmae6k6iZdj2saeXDiw7Wu+t7M8igtZuhYuTfxuR5tVf69K+jiHPsrNOIgrLBMWgN+BPy70k=

iJ85vSXQBBHBw94VNeq6eMy7ThPXqbPWYsnBL+0A6+xyz8c6DbB+yf3GboQ54NUbETI87LdnI+oU6IqFxpEpRiDVnhNwK/Z4zDx5tqn7yMNQ9rk5XcqIuVr3D9wpd2KU

BBJWkULQKXOKIgBNo1Kf75ZIz4Bid3wdSxQL2AwoPrKMXjKPkT5hdLtnrWwkCnu5

/*eslint no-implicit-globals: "error"*/

foo = 1;

Bar.prototype.baz = function () {
    a = 1; // Intended to be this.a = 1;
};

只读全局变量

IiENc7/uVGfXQxR0H1liLkFYKZPu8K+7gQgCbj6y0ALHJD53Somrg6QT+37d39o2y4VwD9H/2q5TP5MnXALgZxeEu8WEbz/fzaELg72swFVfVrZmAR9X3eHqd1+UfLpK

T/r0ldtmpa661HGDngyiojLlGq4u0cqcupMEx7TkQUBs5WemO667xSFftK3D+0Kq6RKygXEcSGK8GbXLoNidvf/tlFMDNiy7ra8/yKtfRyEzH9vs/PwXYdowoPUyDvb2K4YE5k/eSif6FNT8fT0w9KAwGZrjnFwuKHw5/M6zISRgwGCLGuArkQq2sj61Y/Ac1zLUe7uwW1Z//uJrh1lOk7PH8ECdIxTefNfz7qHlFFTT2KdfFtBT2RlQolJuTlvLheSIJ0Xx7biKDjYO9WWlMtvXs51o+8FK42Msgl9/f52kDYoexoTaUBrmcHZ4f0Cy6MD1aUI7dafceZiYIssdGeYdWb+8oPsRkPIiBKN0SOR1S8PMfhEu4Eqr9P0MKDAZ

    MuKnaskIaxTyqxUKjurs1mK5y8rzVGP/B3sonaU8D2o+qCF3pYek5m/ft6VskEkYqpFqdYH2rBd3mqWA67s++KYKLP0ZYAsTGzvvFgjxHSxNkEZJgLFMyyNtw5lUqmas8R8LAIRY4rONzD1v9lovUmsORTh7Nig6mb7dcsR/jAyptyvT+ixOAbURPI7jJ+S+6oneeeMzhl8FXZKyfsSEMQ==

BBJWkULQKXOKIgBNo1Kf75ZIz4Bid3wdSxQL2AwoPrKMXjKPkT5hdLtnrWwkCnu5

/*eslint no-implicit-globals: "error"*/

/*global foo:readonly*/

foo = 1;

Array = [];
var Object;

const、let 和 class 声明

CMKjTx/wR9PUNrAZkSU03nxLHmtTrmnXZThCX0lrVmEh1gw6XMhChBVWIH58WdH/XDUH7TecphLJ//dSgcy4yZh+7W4VDOSLAfYOT4yP5l2aAhUKG38WizYSq/qbXsQHnkqh317EYEx2q9Lk7pNcX9Kz5C6F/HDdV3gTnvIJni8=

9wJMWCFia+04tCZdpqEvdMLsauxlR3jccevq2yoTZmumoPt5UYfHdESDiquOXhbLcNUWuxlYlw1pt/yaf0uvCnYW10/BBdvZBVVXrlFciNn3awFN3xR3sHT9pG+5Pdu3DSsbENagDUs9kEp5DjqN4yqPiO0xH5oSaaUnToObKOnoJwJY/L8jUYlvCFcqBFq7/12fspkHOSwMirDLZAPWizrpxmHF0XyGjXg1i/hpq1Qowlzd9U6D/3edMIzhX0SJ20jQgQ+cmJFTjRf/6uNLB9+riyGkBcP687g6RVYs9TWtvyZZert0wmjaGyXnPAf/iWuSemIP0Y3JFDRPztHObc8ACDh4tD6pY8BoYSEFJbMPk0i1xaJ+lFD1xXVUieVZXxic6tOj2X5b/p+1bo2pw7AGvq5DrZDxTSetTHXXbG57nrhG57jh9nIs55ARf246wbuRwjZZ5mNH9xucoJuEcV7bl8dWS4+O3V+Gt7vMld8=

d2oSxSXVNUa8RxTrXZ+sOD7VBAWABN1ToEmIqIOOYg4FP3QEJ52+/w48H6f68jYg302fzFXbF+572zYArQNEKopiLXfAqNf1a03Uei+goQGlws5YlHQOwTT5jljsydMlQmcV9NFVtUQPvGOU71OBoQ==

PwR5DrJrN80min96hKNCvTqBajX6zEp21bd6ZYasO8xyrbirCJ/1RW7fHcBeHlWWdnzjJ7UBX1ePt0KQEFMF2IcBDgbTx5Et2IRZvp8RhDKrosoJ2qu5CvRs7SBbl7j6RK8qyOqTxkN2la7JNLY0XTfzxbrJniQByHvoZnSzllw=

/*eslint no-implicit-globals: ["error", {"lexicalBindings": false}]*/

const foo = 1;

let baz;

class Bar {}

BBJWkULQKXOKIgBNo1Kf75ZIz4Bid3wdSxQL2AwoPrJ0sQsNYJWQOsIlGUIcwcj0sWIe0ztWSsDtdnWhMDoF1uO1CJ/QDC+Gb/3pr+rqcKxpwP18JMJJ9y99dGkinwE3QzeWj4S4DDIbOKOdEnC5wg==

/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

const foo = 1;

let baz;

class Bar {}

PwR5DrJrN80min96hKNCvTqBajX6zEp21bd6ZYasO8xyrbirCJ/1RW7fHcBeHlWWdnzjJ7UBX1ePt0KQEFMF2IcBDgbTx5Et2IRZvp8RhDI9YJFAkmOt1QNDLFodYfICl06xpIkKBa2OVXqyk2bdCA==

/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

{
    const foo = 1;
    let baz;
    class Bar {}
}

(function() {
    const foo = 1;
    let baz;
    class Bar {}
}());

m106ZaGEabo8Dwy3zV9KI+T1iaAXJ5PN0wG85gyGkEDN5OHCQcyHvqDL1U3kuZkRXonzF2wqO3DpgjJ1WumUYci83xTf7gBCdQS73OCS++hQWlV/dohFHSc+HCkWo33vBCi+V2mjYt+62xcDGLirEptSU7dYnb18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KXOKIgBNo1Kf75ZIz4Bid3wdSxQL2AwoPrJ0sQsNYJWQOsIlGUIcwcj0sWIe0ztWSsDtdnWhMDoF1uO1CJ/QDC+Gb/3pr+rqcKxpwP18JMJJ9y99dGkinwE3QzeWj4S4DDIbOKOdEnC5wg==

/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

const MyGlobalFunction = (function() {
    const a = 1;
    let b = 2;
    return function() {
        return a + b;
    }
}());

PwR5DrJrN80min96hKNCvTqBajX6zEp21bd6ZYasO8xyrbirCJ/1RW7fHcBeHlWWdnzjJ7UBX1ePt0KQEFMF2IcBDgbTx5Et2IRZvp8RhDI9YJFAkmOt1QNDLFodYfICl06xpIkKBa2OVXqyk2bdCA==

/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

window.MyGlobalFunction = (function() {
    const a = 1;
    let b = 2;
    return function() {
        return a + b;
    }
}());

exported

iwVjfENcNRQ4siQT7FcJiWlTD3XOeee2mM6Zw7av5e7FXtFL9FLgzZ28KpFQG1xC4aBn5SluuVwAlE2gM4Be/4tOUEDaE8mTCH/vl5HWDmsCP9jKdxcLjaAJ6I66ZpKexG2TgGBcNzvYncAiCrxRo4LDyDXyENofUuYIa6GxcputTXEGSadwxdlNqAc/eAx/B8XksZHCpHys1w6vTG30wxhjgtAIpcSNU7Hq+crFhUXYMOVVgaXa/x9UAm5aHGD0Ocs0JnunqhhNVSaLir3EibNO79inRWBT8RFxgMRoUOKt8Uu5Xz8d/SuaykdIZNq04nhnNt52v0gRkD4IHiK/2qI2iIAPbOInVO36s3+lmaOQXsEBw15gzYLRIlUHApcy

B5/0Qh7HYmrzdRfb7qOWyTvLyDQTunyO8JHy1wHhhk2+KHb9Yi1sTwKnMKMxJuzp6/nX5rQR/85x1GQr+rO0fj/TddMqP9AKnzTgBg/Q4s4=

/* exported global_var */

var global_var = 42;

何时不使用

33G6CqtmrS7mmddnEtHnkodSPUrojDHzqk2V4Atvmc3JThJNckh8AMH+GGSMUjOeWJfKi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