Solved

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

Posted on 2012-03-18
3
176 Views
Last Modified: 2012-03-18
I get the following error when I run the code below:

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

<html>
<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>";
                  document.write(table);
}}
     
</script>
</head>
<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",""));
cust1.showCustomer();
</script>
</body>
</html>

Can someone please advise what I am doing wrong?

thanks
0
Comment
Question by:jecommera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37735041
yes, you cannot make a constructor of this class.

you need to use it as is

like Customer.setName();
0
 

Author Comment

by:jecommera
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?

thanks
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending ASP to server side 8 32
window close link 7 36
Jquery if else not working ... WHY? 5 26
Why is my $_POST not going to results page 10 41
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

730 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