1

I saw this JavaScript code in some pages a few times:

<script>
      document.write('<script src=js/vendor/'
        + ('__proto__' in {} ? 'zepto' : 'jquery')
        + '.js><\/script>');
    </script>

I would like to know what exactly it does. I guess it serves to include jquery and zepto libraries.

SimonD
  • 1,441
  • 2
  • 18
  • 30

4 Answers4

4

This code appears to be checking for the existence of __proto__ on objects, and pulling in Zepto if available and jQuery when not. Zepto like to keep their codebase small and as a result only really support modern browsers.

According to this (under the browser-side javascript section) __proto__ is not supported by all browsers.

juco
  • 6,331
  • 3
  • 25
  • 42
1

It checks if the browser has __proto_ as part of its objects, if yes, it includes zepto.js, else it includes jquery.js I believe that __proto__ is supported in all major browsers, but there may be a few edge cases

The only browsers that I know of that don't support __proto__ are IE <= 8 and Opera

scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
1

there are some browsers (e.g. IE10-) which do not support Zepto,

so this statement checks if __proto__ is defined for a browser, if not then fallback to jQuery

gaurav5430
  • 12,934
  • 6
  • 54
  • 111
0

{} declares a new object in Javascript, mostly it equals new Object().

in operator detects whether the right operand has a member (function, property, etc).

Zepto is lightweight and has the same APIs as jQuery, but it only supports modern browsers. In old browsers like IE6, __proto__ is not supported, so it falls back to jQuery for compatibility. If browser supports such feature, it loads Zepto instead.