2

I'm getting the values undefined even though it's populated. I think I've already populated it but feels like something is going wrong with the object's instance. That'd be great if someone can explain that where I'm going wrong.

function Student(fn, sn, a, d)
{
  this.firstName = fn;
  this.lastName = sn;
  this.age = a;
  this.degree = d;
  this.displayStudent = displayStudent;
}

function displayStudent()
{
  console.log(this.fn);
  console.log(this.sn);
  console.log(this.a);
  console.log(this.d);
}

var studentObj = new Student("d", "r", 20,
                         "bachelors of science");
studentObj.displayStudent();

9
  • Why do you think this.fn and so on would refer to this.firstName and so on? Commented Mar 7, 2019 at 16:12
  • I tried Studen["fn"] instead of this.fn inside the function declaration but still the value I'm getting is undefined. Thanks. Commented Mar 7, 2019 at 16:12
  • @dr97 but you are setting this.firstName - you don't have a this.fn property on Student. Commented Mar 7, 2019 at 16:13
  • 1
    @VLAZ I got your point. thanks. I was making a dumb mistake. Commented Mar 7, 2019 at 16:17
  • 1
    @MaxouMask How would you use "real classes" in TS if TS is compiled into JS every time? You're only running JS in the end, so if you can't have "real classes" there, then it's not possible to have "real classes" in TS since you never RUN TS Commented Mar 7, 2019 at 16:22

2 Answers 2

1

I think it's just a typo, your code should be like this:

function Student(fn, sn, a, d)
{
  this.firstName = fn;
  this.lastName = sn;
  this.age = a;
  this.degree = d;
  this.displayStudent = displayStudent;
}

function displayStudent()
{
  console.log(this.firstName);
  console.log(this.lastName);
  console.log(this.age);
  console.log(this.degree);
}

var studentObj = new Student("d", "r", 20,
                         "bachelors of science");
studentObj.displayStudent();

In your code you tried to print Student's "constructor" attributes instead of Student's "object" parameters you set.

Sign up to request clarification or add additional context in comments.

2 Comments

It works, thanks a lot. Sorry for making such silly mistake.
That is exactly why SO is here @dr97. You should accept this answer before any other false and misleading information is put here.
1

You are trying to display the Student constructor parameters instead of displaying the Student properties, a method declared outside the constructor does not have access to the constructor´s parameters. To display the Student properties with the displayStudents() method do this.

function Student(fn, sn, a, d)
{
  this.firstName = fn;
  this.lastName = sn;
  this.age = a;
  this.degree = d;
  this.displayStudent = displayStudent;
}

function displayStudent()
{
  console.log(this.firstName);
  console.log(this.lastName);
  console.log(this.age);
  console.log(this.degree);
}

var studentObj = new Student("d", "r", 20,
                         "bachelors of science");
studentObj.displayStudent();

Also when you declare methods outside the object constructor you should use the prototype property like this.

function Student(fn, sn, a, d)
{
  this.firstName = fn;
  this.lastName = sn;
  this.age = a;
  this.degree = d;
}

Student.prototype.displayStudent = function()
{
  console.log(this.firstName);
  console.log(this.lastName);
  console.log(this.age);
  console.log(this.degree);
}

var studentObj = new Student("d", "r", 20,
                         "bachelors of science");
studentObj.displayStudent();

8 Comments

dont you know prototypes are bad
@KunalMukherjee - Why? What makes prototypes bad?
if you make your own custom prototype without knowing that it alread exists natively then it messes up the native one
Sure, but that doesn't make prototypes bad, that makes over writing native objects bad. Since Student is not a native object, using prototypes is just fine. It is the nature of the language and how it works. If you want a reference to how much prototypes are used, look at the jQuery source code.
@KunalMukherjee. Don´t satanize programmatic structures or tools to the extent of not using them ever, they are there for a reason and I don´t see the problem of using prototype here. prototype is a tool used for inheritance in javaScript
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.