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

Namespace VS scope

Is it true to conclude that in each scope (global or specific), and without any exceptions, we can have only one namespace for each name?

For example:

  1. myVar for a let, won't be able to be used again in any other thing in the code (even if it's not a var of any kind).
  2. myFunc for a function, won't be able to be used again in any other thing in the code (even if it's not a named function of any kind).

Dave McFarland Joel Kraft

2 Answers

This sounds like your lesson will soon be leading up to javascript closures and javascript classes. Here is an article i found on closures http://javascriptissexy.com/understand-javascript-closures-with-ease/#

Also here is a small demonstration i made. I not sure my example is expressing closures, as i am still new to the technique. However you can see that the same function name and variable can be used throughout, without overwriting each other https://jsfiddle.net/9s1yhda6/19/

Hope that helps :thumbsup:

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Ben Aharoni

When you declare a variable like this let myName = 'Dave'; you cannot redeclare that variable again using let. For example, this creates an error:

let myName = 'Dave';
let myName = 'Ben'; // produces an error 

However, you can redefine the value of that variable:

let myName = 'Dave';
myName = 'Ben'; // new vale of myName is 'Ben'

You can even reassign a different type of value to that variable:

let myName = 'Dave';
myName = function(name) {
    return "My name is " + name;;
};

You can use the same identifier (that's the name you give the variable) when declaring different variables as long as they live in their own scope -- like the global scope of the program, or the local scope of a function. For example, there is no problem with this code, even though the same identifier is used twice:

let myName = 'Dave'; // global scope
let sayFrank = function() {
  let myName = 'Frank'; // no error, local scope
  return myName;
};
console.log(myName); // "Dave"
console.log(sayFrank()); "Frank"

It's OK because the two myName variables live in different scopes. Check out this video Variable Scope for a refresher on scope.

For a more in-depth discussion of how variables live in different scopes check out this workshop: Understanding Closures in JavaScript.

Thank you for this blessed detailing Dave. I hence conclude that namespaces are scope-dependant. I didn't quite make the connection between scopes and namespaces and you lit me to that, so thanks!