-1

My site has several ajax functions essentially identical (except different variable names, etc.) to the following:

function ajaxFunction(string) {
var ajaxRequest;
    try {
        ajaxRequest = new XMLHttpRequest();
    } catch (e) {
        try {
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
        try {
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {   
                alert("Your browser broke!");
                return false;
        }
        }
    }//Anonymous function starts here
ajaxRequest.onreadystatechange = function() {
    if (ajaxRequest.readyState == 4) {
            var ajaxDisplay = document.getElementById('div');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
} //and ends here
    var QueryAjax = "?string=" + string;
    ajaxRequest.open("GET", "AJAX_File.php" + QueryAjax, true);
    ajaxRequest.send(null);
}

For the indicated anonymous function, the javascript lint found at http://www.javascriptlint.com/online_lint.php warns me I should add a semicolon at the end of this function (before the comment //and ends here in the code above) and that the function does not always return a value; the stricter lint found here: http://www.jslint.com/ warns me ajaxRequest.readyState == 4 should instead be ajaxRequest.readyState === 4.

Does anyone know how detrimental (if at all) the lack of semicolon and lack of "=== 4" are? Also, why does the function not always return a value? My site uses about 50 ajax functions similar to the one above and seems to perform great, but perhaps making these changes will speed up the execution of the functions? Any tips/advice would be greatly appreciated!

  • What does an "anonymous function" have to do with either concept? And why are you simultaneously asking about `===` and `;` (which are two difference concepts)? Anyway, you don't need the semicolon because of [ASI (Automatic Semicolon Insertion)](http://inimino.org/~inimino/blog/javascript_semicolons) .. but, as much as I love ASI, I always include semicolons these days *simply* because Many JS [Developer] Tools Are Too Stupid To Work Without Semicolons. – user2864740 Jan 10 '14 at 05:32
  • Why are you running your code through JSLint if you don't intend to follow it's advice? The answer to your question is: It's as important as you think JSLint's advice is. We can't determine that for you. – user229044 Jan 10 '14 at 06:03
  • Using JSLint to see if in ~7000 lines of functions I had any gross errors....found a few. I just wasn't sure how important the warnings were since my functions seemed to work great, hence the title of my post above – The One and Only ChemistryBlob Jan 10 '14 at 06:06
  • Would the downvoting person care to explain why? – The One and Only ChemistryBlob Jan 10 '14 at 06:08

2 Answers2

1

jslint tools in general suggest coding best practices, but it doesn't mean the code won't work.

In your case, the tool warns you about the semicolon:

ajaxRequest.onreadystatechange = function() {
   ...
}  //Semicolon lack

because it's an assignment (due to the = sign). It won't fail, but it's a good practice to put a semicolon after an assignment.

and the warning:

 warning: function ajaxFunction does not always return a value

is because it's a good practice (at least according to jslint) to always return a value from a function (at least a return undefined; or return null;).

The === advice is, briefly because == can be tricky or return unexpected things some times, for example null == undefined returns true, but null === undefined returns false. === is a strict comparison operator (checks equality on value and data type)

Hope this helps. Cheers

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
1

Do your feature testing once and save the result rather that on each invocation.

In your case == makes no difference when compared to ===, know the difference.

== performs coercion and checks equivalence, === perform no coercion and checks equivalence Comparison Operators

Most if not all environments handle ASI, but it is good practice to use them. See this answer What are the rules for JavaScript's automatic semicolon insertion (ASI)?

So you could rewrite it something like this

/*global document, alert, XMLHttpRequest, ActiveXObject */

var ajaxFunction = (function () {
    'use strict'; // if you want

    var ajaxRequest;

    try {
        ajaxRequest = new XMLHttpRequest();
    } catch (e1) {
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e2) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (ignore) {}
        }
    }

    return function (string) {
        if (!ajaxRequest) {
            alert("Your browser broke!");
            return false;
        }

        ajaxRequest.onreadystatechange = function () {
            if (ajaxRequest.readyState === 4) {
                document.getElementById('div').innerHTML = ajaxRequest.responseText;
            }
        };

        ajaxRequest.open("GET", "AJAX_File.php?string=" + string, true);
        ajaxRequest.send(null);
        return true;
    };
}());
Community
  • 1
  • 1
Xotic750
  • 22,914
  • 8
  • 57
  • 79