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
Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mouse event to control image and transparency. 4 34
Javascript the "if condition with Or" 8 29
on window close, not beforeunload 6 17
JQuery Syntax... 4 33
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

856 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