Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange javascript code

I have found this code snippet:

;       
100% function($) {                                        // WTF?
    var _true_ = true;                                    // WTF?
    var _false_ = false;                                  // WTF?
    var go = function(location, date) {
        location || (location = {});  
        var result = _false_;
        if (date && date.day) {       
            result = geoService.go(location, date);
        }
        return !!result;              
    }
    var process = function(func) {
        var args = [].prototype.slice.call(arguments, 1); 
        return function() {                               
            return func.apply(this, args);
        }
    }
    // ...
}(jQuery, undefined);                                     

In here: http://www.dofactory.com/products/javascript-jquery-design-pattern-framework (sorry, no id-s have been found on the page)

I don't understand what these parts are doing:

  1. the "100%" in the second line
  2. the var _true_ = true; and var _false_ = false; assignments in the 3-4 lines

I'm curious, what is the purpose of these.

like image 883
Herbertusz Avatar asked Jan 25 '15 13:01

Herbertusz


2 Answers

the "100%" in the second line

It's the number 100 followed by a modulus operator. It's not used for anything (since the result isn't captured) other than to force the right hand side to be treated as a function expression instead of a function declaration.

It's a very uncommon and unintuitive approach that I've never seen before.

It is more usual to wrap the function expression in parens or precede it with a not operator.

the var true = true; and var false = false; assignments in the 3-4 lines

The author appears to be trying to draw attention to the uses of true and false by copying them to variables that include non-alpha numerica characters in the name instead of using literals throughout. Again, this is very odd and not something I've ever seen before.

like image 128
Quentin Avatar answered Sep 19 '22 05:09

Quentin


It looks like it is a collection of wrongly used "best practices" which not led to exceptions but definitely odd and obscured. Look at second and last lines. There is best practice used exactly vice versa:

(function ($, undefined){
    // do the stuff 
})(jQuery);

undefined here will be the real undefined because when function call there is no second argument. But what on Earth can be the reason pass the "undefined" argument to the function and do not use it? It looks like a prank.

The same thing is on 5 line: it looks (and actually acts) as "default argument" assigning but done in strange manner (traditionally and more obviously it used as location = location || {};). I beleive that only reasons to write it that way it done can be obfuscation, joke or misunderstanding.

The same thing is with 100%. You can use any operators to indicate function expression. The most common way is to use parenthesis. But often you can also meet:

!function(){

}();

or:

+function(){

}();

but you can also write

42 * function(){

}();

it all acts the same way only parenthesis are most obvious and common.

like image 23
Roman Bekkiev Avatar answered Sep 20 '22 05:09

Roman Bekkiev