Why do I get undefined for this line "console.log(newStudent.fullname);" below?  It's driving me crazy.

<!DOCTYPE html>
	<title>JavaScript Test Page</title>
	<script type="text/javascript" src="date.js"></script>
		console.log("Object-Oriented JavaScript - Student constructor");

		function student(firstName,lastName,ssn,dormName)
			this.fname = firstName;
			this.lname = lastName;
			this._ssn = ssn || 0;
			this.classes = [];
			this.dName = dormName || "A Hall";

		Object.defineProperty(student.prototype, "fullname", {
			get: function() {
				return this.firstName + " " + this.lastName;
		var newStudent = new student("John","Doe","231-31-1313","jjjjj1111");
		console.log("HELLO WORLD");


Open in new window

IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
RobOwner (Aidellio)Commented:
get: function() {
				return this.firstName + " " + this.lastName;

Open in new window

should be

get: function() {
				return this.fname+ " " + this.lname;

Open in new window

this article may help: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_13138-Javascript-is-just-an-Object.html

The scope of firstName and lastName are lost once the constructor has finished.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.