Solved

object.defineProperty()

Posted on 2014-12-17
1
128 Views
Last Modified: 2014-12-22
Why do I get undefined for this line "console.log(newStudent.fullname);" below?  It's driving me crazy.

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

		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");
		console.log(newStudent.fullname);
	</script>
</head>

<body>
</body>
</html>

Open in new window


Thanks!
0
Comment
Question by:Isaac
1 Comment
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40506696
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now