how to access javascript object variables in prototype function

Viewed 19918

I have the following javascript

function person() {
  //private Variable
  var fName = null;
  var lName = null;

  // assign value to private variable
  fName = "Dave";
  lName = "Smith";
};

person.prototype.fullName = function () {
  return this.fName + " " + this.lName;
};

var myPerson = new person();
alert(myPerson.fullName());

I am trying to get an understanding of object orientated techniques in javascript. I have a simple person object and added a function to its prototype.

I was expecting the alert to have "Dave Smith", however I got "underfined underfined". why is that and how do I fix it?

4 Answers
Related