0

I am working on a code that I didn't write personally, in this code there is an anonymous function that was declared without the "var", "let" or "const" keyword. From my understanding of JS, this should raise an error, but it doesn't. The code actually works fine... How could this behavior be explained ?

Here is a simplified sample of the relevant code (the most important part is probably the second half, were the "declaration" of ajax is done):

// WS for Workspace
var WS = (function ($) {
    "use strict";
    var SessionID = 0, /**< current session id */

    //function to retrieve users names from the server
    var getUsersName = function () {
        return new Promise(function (resolve, reject) {
            let DataObject = {"RequestType": "getUsersName","Session_ID": SessionID };
            let successAjax = function (data) {
                (...)
                resolve(UsersName);
            }
            ajax({
                url: "WebServices/Workspace_Ajax.asmx/WebMethodSimple",
                data: JSON.stringify(DataObject),
                origin: "getUsersName"
            }).then(successAjax, reject);
        });
    };

    (...)

    return {// This dictionary contains references to public methods from the object WS
        getBaseURL: getBaseURL,
    };
})(jQuery);



/**
    $.ajax Wrapper for requests to server
    Returns the jqXHR object of the $.ajax Query
    Parameters: - Same as $.ajax method. They are copied into the final parameter object passed to the ajax call.
                - origin: Specify the origin of the call. This parameter is sent to logApplicationError if an error occures.
                - type, dataType, contentType, are already set inside ajax and cannot be overriden.
                - success and error will be handed over after checking for the server response

    success function will be called if the ajax request ends successfully (no errors at all).
    error function will be called   - if the ajax request ends successfully, but the resulting data has an HTML_ForError entry.
                                    - if the ajax request doesn't end successfully (error code).
*/
ajax = function (options = {}) {

    let defaults = {
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",

        success: function (data, status, jqXHR) { (...) },
        error: function (xhr, status, error) { (...) };

    let settings = $.extend({}, options, defaults); // Ensure defaults properties are not overriden, nor options properties

    return $.ajax(settings);
};

Note that "ajax" is defined without the "var", "let" or "const" keyword. I have searched for an other ajax declaration with any of those keyword, in to the whole project using the searching tool of Visual Studio, but I found nothing. I tried "Go To Definition" to jump to an eventual alternative declaration but got the following message : "Cannot navigate to the symbol under the caret".

Despise missing any clear declaration, the code works without any issue. Adding a "var" before "ajax" does not change the code behavior, as if it was optional. Why is that ?

Broader context: my aim is to parse all functions of the source-code for an auto-documentation tool I am coding (using the TypeScript compiler API), but this anonymous function that has no clear declaration is causing me some problems.

ANSWER: Because of the "use strict" line present in many project I've worked on, I never had the occasion to work with Implicit Globals. By default, a variable can be declared without any keywords apparently.

Pierre
  • 55
  • 8

0 Answers0