Conditions | 1 |
Paths | 1 |
Total Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 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( window, document, undefined ) { |
||
2 | "use strict"; |
||
3 | |||
4 | var Plugin = function( videoEl, options ) |
||
5 | { |
||
6 | this.video = videoEl; |
||
7 | if( this.video ) { |
||
8 | this.hasLoaded = typeof YT === undefined; |
||
|
|||
9 | this.options = castor._extend( this.defaults, options ); |
||
10 | this.player = null; |
||
11 | this.init(); |
||
12 | } |
||
13 | }; |
||
14 | |||
15 | Plugin.prototype = |
||
16 | { |
||
17 | defaults: { |
||
18 | iframe: '.video-embed', |
||
19 | poster: '.video-poster', |
||
20 | spinner: '.video-spinner', |
||
21 | }, |
||
22 | |||
23 | init: function() |
||
24 | { |
||
25 | if( this.hasLoaded === false ) { |
||
26 | this.injectScript(); |
||
27 | } |
||
28 | this.onYouTubePlayerAPIReady(); |
||
29 | this.video.querySelector( this.options.poster ).classList.add( 'hide' ); |
||
30 | }, |
||
31 | |||
32 | injectScript: function() |
||
33 | { |
||
34 | this.hasLoaded = true; |
||
35 | var tag = document.createElement( 'script' ); |
||
36 | tag.src = "//www.youtube.com/player_api"; |
||
37 | var firstScriptTag = document.getElementsByTagName( 'script' )[0]; |
||
38 | firstScriptTag.parentNode.insertBefore( tag, firstScriptTag ); |
||
39 | }, |
||
40 | |||
41 | onReady: function() |
||
42 | { |
||
43 | var spinner = this.video.querySelector( this.options.spinner ); |
||
44 | this.player.playVideo(); |
||
45 | setTimeout( function() { |
||
46 | spinner.classList.add( 'hide' ); |
||
47 | }, 1000 ); |
||
48 | }, |
||
49 | |||
50 | playerHasLoaded: function() |
||
51 | { |
||
52 | this.player = new YT.Player( this.video.querySelector( this.options.iframe ), { |
||
53 | events: { |
||
54 | onReady: this.onReady.bind( this ), |
||
55 | }, |
||
56 | }); |
||
57 | }, |
||
58 | |||
59 | onYouTubePlayerAPIReady: function() |
||
60 | { |
||
61 | var self = this; |
||
62 | var youtubeApiReadyExists = typeof window.castor.YouTubeAPIReady === undefined; |
||
63 | setTimeout( function() { |
||
64 | if( typeof window.onYouTubePlayerAPIReady !== undefined ) { |
||
65 | if( !youtubeApiReadyExists ) { |
||
66 | window.castor.YouTubeAPIReady = []; |
||
67 | } |
||
68 | window.castor.YouTubeAPIReady.push( window.onYouTubePlayerAPIReady ); |
||
69 | } |
||
70 | window.onYouTubePlayerAPIReady = function() { |
||
71 | self.playerHasLoaded(); |
||
72 | if( youtubeApiReadyExists ) { |
||
73 | if( window.castor.YouTubeAPIReady.length ) { |
||
74 | window.castor.YouTubeAPIReady.pop()(); |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | }, 2 ); |
||
79 | }, |
||
80 | }; |
||
81 | |||
82 | castor.YouTube = Plugin; |
||
83 | |||
84 | })( window, document ); |
||
85 |
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.