Conditions | 1 |
Paths | 1 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 | (function () { |
||
2 | |||
3 | function hitchClick(e) { |
||
4 | var modalPaywall = document.querySelector('#modalPaywall'); |
||
5 | modalPaywall.classList.toggle('modal-open'); |
||
6 | modalPaywall.dataset.url = e.target.dataset.url; |
||
7 | e.preventDefault(); |
||
8 | } |
||
9 | |||
10 | function submitPaywall(e) { |
||
11 | var modalPaywall = document.querySelector('#modalPaywall'), |
||
12 | url = modalPaywall.dataset.url, |
||
13 | httpRequest = new XMLHttpRequest(); |
||
|
|||
14 | |||
15 | if (!httpRequest) { |
||
16 | alert('Giving up :( Cannot create an XMLHTTP instance'); |
||
17 | return false; |
||
18 | } |
||
19 | httpRequest.onreadystatechange = function () { |
||
20 | if(httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) { |
||
21 | var response = JSON.parse(httpRequest.responseText); |
||
22 | window.location = response.url; |
||
23 | } |
||
24 | }; |
||
25 | httpRequest.open('GET', url); |
||
26 | httpRequest.send(); |
||
27 | e.preventDefault(); |
||
28 | } |
||
29 | |||
30 | function paywall() { |
||
31 | var hitchLinks = document.querySelectorAll('.hitch-link'), |
||
32 | submitter = document.querySelector('.paywall-submit'); |
||
33 | for(var i=0;i<hitchLinks.length;i++){ |
||
34 | hitchLinks[i].addEventListener('click', hitchClick, false); |
||
35 | } |
||
36 | if (submitter !== null) { |
||
37 | submitter.addEventListener('click', submitPaywall); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | function closeModal(e) { |
||
42 | var modalId = e.currentTarget.dataset.modal, |
||
43 | modal = document.querySelector('#'+modalId); |
||
44 | modal.classList.remove('modal-open'); |
||
45 | } |
||
46 | |||
47 | function initCloser() { |
||
48 | var modalCloser = document.querySelectorAll('.modal-closer'); |
||
49 | for(var i=0;i<modalCloser.length;i++){ |
||
50 | modalCloser[i].addEventListener('click', closeModal, false); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | initCloser(); |
||
55 | paywall(); |
||
56 | })(); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.