iwato Posted April 7, 2018 Share Posted April 7, 2018 (edited) BACKGROUND: Earlier I posted a script from Matomo that serves as a counter for page hits and various other bits of information gleaned from a user's behavior on a webpage that bears the script. Although I now understand what the code does, I would like to understand its purpose. In particular, I would like to understand the probable reason for assigning to a variable another variable with the same name. The full code is given as: <!-- Matomo --> <script type="text/javascript"> var _paq = _paq || []; _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//{$PIWIK_URL}/"; _paq.push(['setTrackerUrl', u+'piwik.php']); _paq.push(['setSiteId', {$IDSITE}]); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code --> The code of interest is var _paq = _paq || []; I find this piece of code problematic for three reasons: It assumes that this variable could already exist, but I have no idea where it could come from. It it does already exist what happens to the pre-existent with the same name. How does it know which value to choose? Is there a latent selection mechanism that tells the assignment operator to choose the second value when the first is undefined? Please advise. Roddy Edited April 8, 2018 by iwato Link to comment Share on other sites More sharing options...
Ingolme Posted April 9, 2018 Share Posted April 9, 2018 The OR operator in Javascript returns the leftmost operand that evaluates to a boolean true value. In the following code, the variable x gets set to 5 because 0 is false. var x = 0 || 5; When you assign a variable to itself nothing changes: var x = x; When a variable does not exist, its value is undefined which evaluates to boolean false. In the following code the variable x gets set to 5 because an undefined variable is false. var x = x || 5; In conclusion, the code var x = x || []; will do nothing when x already exists but will assign an empty array to x if it does not already exist. The reason that people creating embeddable scripts do this is to prevent the program from breaking when somebody embeds their code more than once in the same document. 1 Link to comment Share on other sites More sharing options...
iwato Posted April 9, 2018 Author Share Posted April 9, 2018 Revisiting the Matomo insert <!-- Matomo --> <script type="text/javascript"> var _paq = _paq || []; _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//{$PIWIK_URL}/"; _paq.push(['setTrackerUrl', u+'piwik.php']); _paq.push(['setSiteId', {$IDSITE}]); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code --> If what you said is true, then the following statement would be true would it not? STATEMENT: Were the same script to be entered twice on the same page, then the contents of the first completed _paq array would be passed onto the _paq array of the second entry and everything would be counted twice. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 9, 2018 Share Posted April 9, 2018 Right. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now