最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript "this" issue - Stack Overflow

programmeradmin6浏览0评论

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 call new NewUser(). You're not setting the name 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 to this.name and SomeUser.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
Add a ment  | 

3 Answers 3

Reset to default 13

You 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();
发布评论

评论列表(0)

  1. 暂无评论