-7

I could see some piece of code for while am searching for infinity scroll. In the below piece of code am not able get a line of code.

var lazyload = lazyload || {};

var lazyload = lazyload || {};

(function($, lazyload) {

    "use strict";

    var page = 2,
        buttonId = "#button-more",
        loadingId = "#loading-div",
        container = "#data-container";

    lazyload.load = function() {
        alert("Invoked");
        var url = "./" + page + ".html";

        $(buttonId).hide();
        $(loadingId).show();

        $.ajax({
            url: url,
            success: function(response) {
                if (!response || response.trim() == "NONE") {
                    $(buttonId).fadeOut();
                    $(loadingId).text("No more entries to load!");
                    return;
                }
                appendContests(response);
            },
            error: function(response) {
                $(loadingId).text("Sorry, there was some error with the request. Please refresh the page.");
            }
        });
    };

    var appendContests = function(response) {
        //var id = $(buttonId);

        $(buttonId).show();
        $(loadingId).hide();

        $(response).appendTo($(container));
        page += 1;
    };

})(jQuery, lazyload);
feeela
  • 29,399
  • 7
  • 59
  • 71

3 Answers3

4

The code isn't jQuery specific. It means "If lazyload is already defined, assign its existing value (a no-op). If not, assign an empty object ({}).".

joews
  • 29,767
  • 10
  • 79
  • 91
1

[] is an shortcut to new Array(). {} is an shortcut to new Object().

var lazyload = lazyload || {};

This means: "set lazyload variable as "lazyload" OR empty object."

Elias Soares
  • 9,884
  • 4
  • 29
  • 59
1
var myVar = myVar || {};

// is the same as this

if(myVar == undefined)
    myVar = {};  // initialize variable as empty object

// or

var myVar = (myVar != undefined) ? myVar : {};

|| is an or operator. You can basically read the statement as a choice selection: "my variable or an empty object"

This just ensures that the variable is not undefined

m.e.conroy
  • 3,508
  • 25
  • 27