0

        settings.supportsCssTransitions = (function (style) {
            var prefixes = ['Webkit', 'Moz', 'Ms'];
            for (var i = 0, l = prefixes.length; i < l; i++) {
                if (typeof style[prefixes[i] + 'Transition'] !== 'undefined') {
                    return true;
                }
            }
            return false;
        })(document.createElement('div').style);

Why use the '()' to Bracket the anonymous funcion? what's the use of (document.createElement('div').style)? Is that every time I execute settings.supportsCssTransitions, ‘document.createElement('div').style’ will be the default paremeter passed into the anonymous funcion? Could someone explain for me?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Jswq
  • 758
  • 1
  • 7
  • 23
  • it's an IIFE, http://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript – Ramanlfc Dec 29 '15 at 04:07

1 Answers1

1

This is a function that tests whether the executing browser supports certain CSS properties.

What this construct:

var result = (function(arg) { ...; return smth; })(arg)

does is create an anonymous function and execute it immediately with argument arg, to then assign its return value to result.

The same result could be achieved with a succession of top-level statements, but doing it like this is better formatting because the variables used inside the function are kept inside it.

The document.createElement('div').style bit simply creates a dummy HTMLElement and pass its style property to the function which then analyzes it.

lleaff
  • 4,249
  • 17
  • 23