Avatar of jecommera
jecommera
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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

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
JavaScript

Avatar of undefined
Last Comment
Gurvinder Pal Singh

8/22/2022 - Mon
Gurvinder Pal Singh

yes, you cannot make a constructor of this class.

you need to use it as is

like Customer.setName();
jecommera

ASKER
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
ASKER CERTIFIED SOLUTION
Gurvinder Pal Singh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23