Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,650 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 :)