Can someone please see what I am doing wrong with my JS literal object

Posted on 2012-03-18
Medium Priority
Last Modified: 2012-03-18
I get the following error when I run the code below:

Customer is not a constructor cust1 = new Customer();

<head><title>Object object</title>
<script type="text/javascript">

var Customer = {
            name : undefined,
            gender : undefined,
            photo : undefined,
            occupation : undefined,
            setName : function(name) {
                  this.name = name;
            setGender : function(gender) {
                  this.gender = gender;
            setPhoto : function(photo) {
                  this.photo = photo;
            setOccupation : function(occupation) {
                  this.occupation = occupation;
            getName : function() {
                  return this.name;
            getGender : function() {
                  return this.gender;
            getPhoto : function() {
                  return this.photo;
            getOccupation : function() {
                  return this.occupation;
            showCustomer : function() {
                  var table = "<table border='1'><th>Customers</th>";
                  table += "<tr><td>Name : </td><td> " + this.getName() + "</td></tr>";
                  table += "<tr><td>Gender : </td><td> " + this.getGender() + "</td></tr>";
                  table += "<tr><td>Photo : </td><td> " + this.getPhoto() + "</td></tr>";
                  table += "<tr><td>Occupation : </td><td> " + this.getOccupation() + "</td></tr>";
                  table += "</table>";
<body bgColor="#EOFFFF">
<script type="text/javascript">

cust1 = new Customer();
cust1.setName(prompt("Please enter the name here",""));
cust1.setGender(prompt("Please enter the gender here",""));
cust1.setPhoto(prompt("Please enter the photo here",""));
cust1.setOccupation(prompt("Please enter the occupation here",""));

Can someone please advise what I am doing wrong?

Question by:jecommera
  • 2
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37735041
yes, you cannot make a constructor of this class.

you need to use it as is

like Customer.setName();

Author Comment

ID: 37735042
OK - so you mean if you use object literals, you can never use constructors?

If you want to use constructors you need to use constructor functions?

LVL 40

Accepted Solution

Gurvinder Pal Singh earned 2000 total points
ID: 37735047
you have defined a function Customer here. You have actually initialized a Customer object. This Customer object cannot have a further instance.

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

619 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