jecommera
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("Plea se enter the name here",""));
cust1.setGender(prompt("Pl ease enter the gender here",""));
cust1.setPhoto(prompt("Ple ase 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
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</
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("Plea
cust1.setGender(prompt("Pl
cust1.setPhoto(prompt("Ple
cust1.setOccupation(prompt
cust1.showCustomer();
</script>
</body>
</html>
Can someone please advise what I am doing wrong?
thanks
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
If you want to use constructors you need to use constructor functions?
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to use it as is
like Customer.setName();