Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JavaScript function addes value to itself as a variable !

I need to understand below function.

var guid = function() {

      var nav = window.navigator;
      var screen = window.screen;
      var guid = nav.mimeTypes.length;
      guid += nav.userAgent.replace(/\D+/g, '');
      guid += nav.plugins.length;
      guid += screen.height || '';
      guid += screen.width || '';
      guid += screen.pixelDepth || '';

      return guid;
  };

What is the line guid += ? How can function add to itself as a variable ?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Ajinkya,

The value isn't been assigned to the function as that is impossible, the guid function has a locally scoped variable called guid which new values are been appended to.

Ajinkya, if you pull up your browser's console and check this out: http://jsfiddle.net/vztmLL4h/ ; You'll see that guid is undefined from within the function's local scope, exactly as Chris has explained.

oh my mistake. thanks for spotting :)