Solved

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

Posted on 2012-03-18
3
172 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now