Firenibbler Posted November 27, 2014 Share Posted November 27, 2014 Hello, I need some help understanding the 'this' keyword.var person=function(name,age){this.name=name;this.age=age;};var Bob=new person("bob",23);for that you can just do Bob.age and get his age but I can't figure out a way to do this:var person=function(name,age){this.name=name;this.age=age;this.printAge=function(){console.log(this.age);};};var Bob=new person("Bob",23);Bob.printAge();Thank You Quote Link to comment Share on other sites More sharing options...
Gio Posted November 27, 2014 Share Posted November 27, 2014 A few points:Your code works, what's the problem? It's a widely-accepted convention to give instantiatable functions (class-like functions like your 'person' function) names that start with a Capital Letter. Whereas instances normally have lower-case names. It's only a convention, but everyone does it so I'd suggest you do it too "this" is the current context. Complicated subject, I recommend reading this explanation Firenibbler 1 Quote Link to comment Share on other sites More sharing options...
Firenibbler Posted November 27, 2014 Author Share Posted November 27, 2014 thanks, I just found out that it was another section that was causing the error. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.