| Conditions | 1 | 
| Paths | 1 | 
| Total Lines | 148 | 
| Code Lines | 55 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** | ||
| 13 | (function (window) { | ||
| 14 | |||
| 15 | 'use strict'; | ||
| 16 | |||
| 17 | var document = window.document; | ||
| 18 | var fns = []; | ||
| 19 | var args = []; | ||
| 20 | var isReady = false; | ||
| 21 | var errorHandler = null; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Call a ready handler | ||
| 25 | * @private | ||
| 26 |      * @param {function} fn | ||
| 27 | */ | ||
| 28 |     var call = function (fn) { | ||
| 29 |         try { | ||
| 30 | // call function | ||
| 31 | fn.apply(this, args); | ||
| 32 |         } catch (e) { | ||
| 33 | // error occured while executing function | ||
| 34 |             if (errorHandler !== null) { | ||
| 35 | errorHandler.call(this, e); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Call all ready handlers | ||
| 42 | * @private | ||
| 43 | */ | ||
| 44 |     var run = function () { | ||
| 45 | var x; | ||
| 46 | |||
| 47 | isReady = true; | ||
| 48 | |||
| 49 | // call all registered functions | ||
| 50 |         for (x = 0; x < fns.length; x = x + 1) { | ||
| 51 | call(fns[x]); | ||
| 52 | } | ||
| 53 | |||
| 54 | // clear handlers | ||
| 55 | fns = []; | ||
| 56 | }; | ||
| 57 | |||
| 58 | /** | ||
| 59 | * Initialize | ||
| 60 | * @private | ||
| 61 | */ | ||
| 62 |     var init = function () { | ||
| 63 |         if (window.addEventListener) { | ||
| 64 | // for all browsers except IE | ||
| 65 |             document.addEventListener('DOMContentLoaded', function () { | ||
| 66 | run(); | ||
| 67 | }, false); | ||
| 68 |         } else { | ||
| 69 | // for IE | ||
| 70 | // code taken from http://javascript.nwbox.com/IEContentLoaded/ | ||
| 71 |             var poll = function () { | ||
| 72 | // check IE's proprietary DOM members | ||
| 73 |                 if (!document.uniqueID && document.expando) { | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | // you can create any tagName, even customTag like <document :ready /> | ||
| 78 |                 var tempNode = document.createElement('document:ready'); | ||
| 79 | |||
| 80 |                 try { | ||
| 81 | // see if it throws errors until after ondocumentready | ||
| 82 |                     tempNode.doScroll('left'); | ||
| 83 | |||
| 84 | // call run | ||
| 85 | run(); | ||
| 86 |                 } catch (e) { | ||
| 87 | window.setTimeout(poll, 10); | ||
| 88 | } | ||
| 89 | }; | ||
| 90 | |||
| 91 | // trying to always fire before onload | ||
| 92 |             document.onreadystatechange = function () { | ||
| 93 |                 if (document.readyState === 'complete') { | ||
| 94 | document.onreadystatechange = null; | ||
| 95 | run(); | ||
| 96 | } | ||
| 97 | }; | ||
| 98 | |||
| 99 | poll(); | ||
| 100 | } | ||
| 101 | }; | ||
| 102 | |||
| 103 | /** | ||
| 104 | * @namespace domReady | ||
| 105 | * | ||
| 106 | * @public | ||
| 107 |      * @param {function} fn | ||
| 108 |      * @return {domReady} | ||
| 109 | */ | ||
| 110 |     var domReady = function (fn) { | ||
| 111 | return domReady.on(fn); | ||
| 112 | }; | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Add code or function to execute when the DOM is ready | ||
| 116 | * @public | ||
| 117 |      * @param {function} fn | ||
| 118 |      * @return {domReady} | ||
| 119 | */ | ||
| 120 |     domReady.on = function (fn) { | ||
| 121 | // call imediately when DOM is already ready | ||
| 122 |         if (isReady) { | ||
| 123 | call(fn); | ||
| 124 |         } else { | ||
| 125 | // add to the list | ||
| 126 | fns[fns.length] = fn; | ||
| 127 | } | ||
| 128 | |||
| 129 | return this; | ||
| 130 | }; | ||
| 131 | |||
| 132 | /** | ||
| 133 | * Set params that will be passed to every ready handler | ||
| 134 | * @public | ||
| 135 |      * @param {Array.<*>} params | ||
| 136 |      * @return {domReady} | ||
| 137 | */ | ||
| 138 |     domReady.params = function (params) { | ||
| 139 | args = params; | ||
| 140 | return this; | ||
| 141 | }; | ||
| 142 | |||
| 143 | /** | ||
| 144 | * Set error callback | ||
| 145 | * @public | ||
| 146 |      * @param {function([Error|string])} fn | ||
| 147 |      * @return {domReady} | ||
| 148 | */ | ||
| 149 |     domReady.error = function (fn) { | ||
| 150 | errorHandler = fn; | ||
| 151 | return this; | ||
| 152 | }; | ||
| 153 | |||
| 154 | // initialize | ||
| 155 | init(); | ||
| 156 | |||
| 157 | // make global | ||
| 158 | window.domReady = domReady; | ||
| 159 | |||
| 160 | })(window); | ||
| 161 |