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 trialDavide Pugliese
4,091 PointsThe scope of a property: to me a very important addition to make - MOD EDIT - Check comments for the correct explanation
MOD EDIT: Please see the comment from Marcus below for the correct explanation of scope.
Hello guys, I think there is a very important addition to make here that is implied and obvious at this point to me, but it may very well not be for the newbies to programming.
In fact when we have something like this:
var me {
my_name: "Davide"
}
var key = my_name
There is a very important thing going on. my_name outside of me keeps being bond to me!
This means that my_name cannot be used anymore for other objects without understanding what is going on with the interpreter.
If we have
var me {
name: "Davide"
}
var you {
name: "Jim"
}
The new value for name
is not Davide anymore but Jim.
This is very important to understand.
In other programming languages we would have:
me.name = "Davide"
you.name = "Jim"
And we would not have any overwriting of the value contained by name
This means that once name has been reserved we cannot use it anymore for other objects in some cases.
This feature is called scope, I heard speaking about the scope of a variable, or the scope of a function, therefore I think it's right to call this the scope of a property.
3 Answers
Marcus Parsons
15,719 PointsPlease do not post posts like this in the Forum as they are misdirecting and can be confusing to new programmers. Thanks.
I am going to go through why you are wrong about the scope of these object key/value pairs though:
1) The following that you posted would not execute properly and does nothing. If you go into your JavaScript console, it will first say that a "Syntax Error" has occured: this is because It needs an "=" operator to assign the key of my_name
to the object me
. Next, when you try to run it, you will get a "Reference Error" because my_name
does not exist. But if you reference me.my_name
, you will find that does exist.
//Will not execute properly
var me {
my_name: "Davide"
}
var key = my_name;
console.log(key);
Here is the proper form:
//Will execute properly
var me = {
my_name: "Davide"
}
var key = me.my_name;
console.log(key);
2) Object and key pairs are unique to that specific object, given that the objects are not part of a prototype. If I create two objects such as "me" and "you" and give them both the key of "name", the "name" key is different in both objects because that key of "name" is unique to that object.
Also, since the "name" key is unique to those objects, you can even declare a variable named "name" and give it the value of "Bob" and you will see console.log
print out the names: Marcus Davide Bob
.
This means that the scope of the "name" key on each object is limited to that object.
You can run this code and prove that:
var me = {
name: "Marcus"
}
var you = {
name: "Davide"
}
var name = "Bob";
console.log(me.name);
console.log(you.name);
console.log(name);
3) You aren't initializing variables the correct way, so your code would not execute. If you notice in the above code block, I have the =
operator in between var me
and the { }
. This is to assign the value(s) within the curly braces to the variable object.
4) In JavaScript, you can reference the values in both me
and you
just as you did. And you will get me.name
is "Marcus" and you.name
is "Davide" just as you would see if you execute the code I pasted for you.
5) If I were to make a prototype of a Person and then create me
and you
objects based on that prototype (and not specify a name
key for them), they would both have the name given from the prototype until I specified a unique "name" key for each object. But since that is not the case here, we don't have to worry about that.
Davide Pugliese
4,091 PointsThank you. :)
Marcus Parsons
15,719 PointsYou are welcome, Davide. Thank you for being open to constructive criticism! :)
Davide Pugliese
4,091 PointsBeing here to learn and being one of millions of developers, etc. I must be open to criticism. While I am learning, there's probably a 13 old kid in Tibet or in the most remote place in the world designing the new Facebook. :D This world is very meritocratic, this is one of the reasons why I like this job, which is a passion for me in the first place. :)
Marcus Parsons
15,719 PointsExcellent answer, Davide. I like your style!
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsBtw, Hugo Paz, I know you are a moderator, so can you please delete this thread or weigh in on what Davide is saying? You are the first name I remembered as a moderator. I appreciate it.
Hugo Paz
15,622 PointsHugo Paz
15,622 PointsHey Marcus,
Thank you for bringing this up. I am leaving this up with an edited title, not only so Davide can see where he is wrong and learn from it but also due to the fact that you provided a in depth explanation that could help other students.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsThank you, Hugo!