Link to home
Start Free TrialLog in
Avatar of jecommera
jecommeraFlag 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
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

yes, you cannot make a constructor of this class.

you need to use it as is

like Customer.setName();
Avatar of 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
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial