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 trialRizwan Renesa
7,805 PointsWhy can't we call the function using foo() ?
function logTeams(){ console.log(this.soccerTeams); }
Portland.foo = logTeams;
Portland.foo(); foo();
Why can't we call the function using foo()? There was a point in the video where this was done and the console spitted out error?
1 Answer
andren
28,558 PointsBecause no function called foo
exists.
In the above code you add a property to the Portland object called foo
and set it equal to a function, since this property stores a function you can call it like a function Portland.foo()
, but that doesn't change the fact that it's just a property stored in the Portland object. foo
by itself is not a variable or function that exists on its own.
Basically if I had an object like this:
var example = {
message: "Hello World!"
}
I could access that message like this example.message
, however I could not simply type message
and access it. Since message
would not be a variable that exists on its own, it's just a property inside the object. The exact same thing is true with foo
.