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! While you're at it, check out some resources Treehouse students have shared here.

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 The Module Pattern in JavaScript

I don't undertsand what's wrong with it: Error: Uncaught ReferenceError: _ is not defined

!function(underscore) { underscore.someawesomemethod = "yay!!!"; console.log(underscore.VERSION); }(_);

2 Answers

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Olivia. All becomes clear once you realise that 'underscore' is actually a JavaScript library (http://underscorejs.org/) which you may or may not have available in your browser (session). The code you have written is correct. In fact you are reading this post in a tab of your browser which may already have 'underscore' installed. For me, in my Chrome browser, if I literally select F12 (important, you must stay on this same tab), to open the devtools Javascript console, then copy/paste in your code, I get back...

1.8.3

true

In contrast, this currently does not work when using Treehouse Workspaces. Maybe something has changed since this video was shot in 2015. I don't know the contents of the teacher's index.html, but if you use this index.html file, it should help.

index.html

<!doctype html>
<html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
    </head>
    <body>
    <script src="pattern.js"></script>
    </body>
</html>

thank you very much neil! now is everything clear .

You have an underscore in between the brackets "(_)"

!function(underscore) { underscore= "yay!!!"; console.log(underscore); }();

I get 'yay' in the console log running this.

thank you Lee for the answer but in exemplo from the video 'Pattern' it is:
!function(underscore) { underscore.someawesomemethod = "yay!!!"; console.log(underscore.VERSION); }(_);

or like this:

!function(){ underscore.someawesomemethod = "yay!!!"; console.log(underscore.VERSION); }();

but i think it is a old version ...