I'm new to Javascript and trying to figure out why console.log isn't printing out the name of the User. I want the function NewUser to take the new Variable (Emily and Jason) and print out their names along with their "life".
My problem seems to be the line "this.name="" ", I don't know how to get this.name to take in the name of the variable for the newuser. I've tried this.name; but that returned undefined.
function NewUser(){
this.name="";
this.life=100;
this.mana=1000;
console.log(this.life);
console.log(this.name);
}
var Jason=new NewUser();
Jason.name="Jason";
var Emily=new NewUser();
Emily.name="Emily";
I'm new to Javascript and trying to figure out why console.log isn't printing out the name of the User. I want the function NewUser to take the new Variable (Emily and Jason) and print out their names along with their "life".
My problem seems to be the line "this.name="" ", I don't know how to get this.name to take in the name of the variable for the newuser. I've tried this.name; but that returned undefined.
function NewUser(){
this.name="";
this.life=100;
this.mana=1000;
console.log(this.life);
console.log(this.name);
}
var Jason=new NewUser();
Jason.name="Jason";
var Emily=new NewUser();
Emily.name="Emily";
Share
Improve this question
asked Jul 31, 2015 at 16:07
SnorlaxSnorlax
4,7759 gold badges42 silver badges72 bronze badges
5
- What is wrong with the code you already have? It looks like it would work. Probably better to pass the initial name as an argument to the constructor, but what you have should also work. – jfriend00 Commented Jul 31, 2015 at 16:11
-
1
The
console.log()
statements are currently inside of the constructor (which runs when you callnew NewUser()
. You're not setting thename
property until after that, though. – jmar777 Commented Jul 31, 2015 at 16:12 - search Stackoverflow using '[javascript] this': 1,309,289 results – KooiInc Commented Jul 31, 2015 at 16:13
-
@KooiInc this isn't actually a problem with using
this
. The problem is just order of execution. OP's references tothis.name
andSomeUser.name
are all correct... (just ordered wrong). – jmar777 Commented Jul 31, 2015 at 16:14 - Thanks a lot for all the feedback! – Snorlax Commented Jul 31, 2015 at 16:29
3 Answers
Reset to default 13You just need to add a parameter to your function, like the following:
function NewUser(name){
this.name=name;
this.life=100;
this.mana=1000;
console.log(this.life);
console.log(this.name);
}
var Jason=new NewUser("Jason");
var Emily=new NewUser("Emily");
Because the console.log statement is being executed when you call the constructor function, at which point the name variable is an empty string.
var Jason=new NewUser(); // console.log called here
Jason.name="Jason";
var Emily=new NewUser(); // console.log called here
Emily.name="Emily";
to fix this, try passing the user name into the function:
function NewUser(username){
this.name=username;
this.life=100;
this.mana=1000;
console.log(this.life);
console.log(this.name);
}
var Jason=new NewUser("Jason");
var Emily=new NewUser("Emily");
You are reading this.name
before write anything to it.
Another way to do it is to create a log
function that you can call later. Example:
function NewUser(){
this.name="";
this.life=100;
this.mana=1000;
this.log = function() {
console.log(this.life);
console.log(this.name);
}
}
var Jason=new NewUser();
Jason.name="Jason";
var Emily=new NewUser();
Emily.name="Emily";
Jason.log();
Emily.log();