/    Sign up×
Community /Pin to ProfileBookmark

why is alert() an implied global in jslint?

Here is a little test function I’ve been playing around with to try and avoid jslint’s ‘implied global’ error when using alert().

Enclosing the alert in a function then calling that function I thought would have made the alert specific to that function not global. Obviously I’m missing something…

Is it possible at all to write an alert() anywhere or in any scope without attracting this error?
(also tried window.alert() and this gives window as the implied global)

[CODE]
function test()
{
var mailingAdd = {
“address” : “34 Short Street”,
“suburb” : “Pleasantville”
};
alert(“The address is ” + mailingAdd.address + “, ” + mailingAdd.suburb + “.”);
}
test();
[/CODE]

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@FangMar 29.2010 — Global VariablesIf a variable is not explicitly declared (usually with the var statement), then JavaScript assumes that the variable was global.[/QUOTE]No need for concern. You can suppress the waring by using/*global alert */
Copy linkTweet thisAlerts:
@berzerkauthorMar 29.2010 — Thanks Fang -

  • - but does /*global alert */ in say -
    [CODE]
    /*global alert */
    function someFunction()
    {
    alert("someText");
    }

    [/CODE]

    actually render that alert to be NOT an implied global or just hide it from jslint?



    It doesn't seem to be really a problem as (of course) the script runs but if it is not really a problem then why would JS wizard Mr. Crockford be so vehemently against 'implied globals'? I've read his discourse on implied globals and he doesn't seem to specify whether /*global alert */ actually renders any subsequent alert() to be non-implied-global - ie - no longer an actual error. Or whether it is just to please jslint...
  • Copy linkTweet thisAlerts:
    @FangMar 29.2010 — [COLOR="Green"]/*global alert */[/COLOR] Just for jslint.

    Try:function someFunction()
    {
    if(document.all) {
    // bla
    }
    }
    [B]Any[/B] global will be flagged in jslint
    Copy linkTweet thisAlerts:
    @berzerkauthorMar 29.2010 — I just tried
    [CODE]
    function test()
    {
    if(document.all)
    {
    alert("some text");
    }
    }
    test();
    [/CODE]


  • - it didn't run.

    but even it this did run would the alert still be actually an implied global - regardless of what jslint says?
  • Copy linkTweet thisAlerts:
    @FangMar 29.2010 — If it's not defined elsewhere it is an implied global

    This lint is friendlier: http://www.javascriptlint.com/online_lint.php
    Copy linkTweet thisAlerts:
    @berzerkauthorMar 29.2010 — http://www.javascriptlint.com/online_lint.php

    great - never knew of this! I like how it throws up the errors in red right under the offending part.


    --------------
    and yes

    function test() {alert("some text") }

    passes in it too.

    But - I still get the feeling - regardless of what methods are used to hide 'implied globals' from jslint - that Crockford would disapprove of
    [CODE]
    function test()
    {
    alert("some text");
    }
    test();
    [/CODE]

    I would love to see how he would write a function with an alert() in it - that he would be happy with and not declare to be 'evil'!

    ...or should I just stop worrying about it...
    Copy linkTweet thisAlerts:
    @KorMar 29.2010 — On the other hand, you could have made that a "local affair" by treating the function [B]test()[/B] as an [I]object[/I]
    <i>
    </i>function test()
    {
    [COLOR="Blue"]this[/COLOR].mailingAdd = {
    "address" : "34 Short Street",
    "suburb" : "Pleasantville"
    };
    alert("The address is " + [COLOR="Blue"]this[/COLOR].mailingAdd.address + ", " + [COLOR="Blue"]this[/COLOR].mailingAdd.suburb + ".");
    }
    test();
    Copy linkTweet thisAlerts:
    @FangMar 29.2010 — On the other hand, you could have made that a "local affair" by treating the function [B]test()[/B] as an [I]object[/I]
    [/QUOTE]
    It's the [I]alert[/I] that is throwing a warning.

    ...or should I just stop worrying about it...[/QUOTE]Yes, window is a global
    Copy linkTweet thisAlerts:
    @berzerkauthorMar 29.2010 — Ok - just that I'll still always worry a little about writing code that the world's foremost JS expert would declare 'evil'.

    (jslint doesn't like ActiveXObject either in my Ajax calls)

    Anyway my 700 line JS file throws up about 40 or so 'implied globals' in jslint but still works fine in the five main browsers. I've been trying for a while now to get around this.

    Still wonder exactly how Crockford would do it. I've recently bought 'Javascript, the good parts' but he doesn't seem to really offer alternatives.

    Like my initial post - the json format doesn't solve it because yes as you say Fang - the alert() is the problem. Even nested in an inner function - still throws the implied global error.
    Copy linkTweet thisAlerts:
    @jakewanMay 02.2011 — You can use the "Assume a browser" flag. This causes jslint to register window in its list of expected global variables. Then, if you really want to silence jslint without resorting to /*global ... */ hints, you can call alert using the window object, e.g.

    [CODE]
    function test() {
    window.alert('Hey!');
    }

    test();
    [/CODE]
    Copy linkTweet thisAlerts:
    @rpg2009May 02.2011 — You can use the "Assume a browser" flag. This causes jslint to register window in its list of expected global variables. Then, if you really want to silence jslint without resorting to /*global ... */ hints, you can call alert using the window object, e.g.[/QUOTE]

    JSLint's an extremely handy tool. I've tended to switch on assume browser anyway, without really knowing what it does. Thanks for that info.

    Another take as you possibly want to drag your code out of the global name space is

    [CODE](function(global, doc){

    doc.elementById('something');
    global.alert('hi');

    }(window, window.document));[/CODE]


    It also helps optimise things a miniscule amount (I believe), as javascript won't have to hunt up the scope chain looking for window and alert.
    Copy linkTweet thisAlerts:
    @omnicityMay 03.2011 — If lint is being a bit too 'extreme' for you, then you might want to look at

    http://jshint.com
    ×

    Success!

    Help @berzerk spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...