The code defines an anonymous function (bit (function (){ ... }) ), and then calls it (without arguments). It then assigns a value to the Browser property of the object, which is supposedly defined outside the code snippet.
You can also define a function somewhere:
function myFunction() { var ua = navigator.userAgent; var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]'; return { IE: !!window.attachEvent && !isOpera, Opera: isOpera, WebKit: ua.indexOf('AppleWebKit/') > -1, Gecko: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1, MobileSafari: /Apple.*Mobile.*Safari/.test(ua) }
and then name it:
var foo = myFunction();
and then assign the value:
... Browser: foo, ...
One of the drawbacks is that you "pollute the namespace" with a function and variable that you will no longer use. The second problem is that you cannot use the value of any locally restricted variables in the definition of a function (an anonymous function behaves like a closure).
Frank Schmitt Mar 11 2018-10-11T00: 00Z
source share