0

So, going through some malware I see that CryptoJS is declared like so:

var CryptoJS = CryptoJS || function(u, p) {
    var d = {},
        l = d.lib = {},
        s = function() {},
        ....stackoverflow doesnt allow me to paste all of the code...
        _createHmacHelper: function(a) {
            return function(b, e) {
                return (new n.HMAC.init(a,
                    e)).finalize(b)
            }
        }
    });
    var n = d.algo = {};
    return d
}(Math);

What I dont understand is this line:

var CryptoJS = CryptoJS || function(u, p)...

Why not simply:

var CryptoJS = function(u, p)...

user1720897
  • 1,035
  • 2
  • 9
  • 21
  • 3
    It's called a polyfill. The point is, if a previous declaration already defined it, then it will short-circuit and skip the polyfill definition. – Patrick Roberts Oct 13 '19 at 04:02

1 Answers1

1

It's commonly called a polyfill definition and it's very common in javascript. If a previous declaration already defined CryptoJS then it will skip what comes after the ||, that is, the definition.

Javier Silva Ortíz
  • 2,426
  • 1
  • 8
  • 19