We help IT Professionals succeed at work.
Get Started

Prototype Javascript-2

rwniceing
rwniceing asked
on
425 Views
Last Modified: 2014-08-25
Dear Experts,

I have set up cook menu price calculation function for two customers, Bob and John to pay after ordering food and drink. It is easy using function, and now the owner restaurant changes the price formula by using the prototype, now the new price is changing to replace the old price formula so they need to pay new price.

But the console.log("Bob pay new price:", guest_Bob_price.price(2,3)); is not working
because  prototype property "price" is conflict to  this.price in the function. It will
be solved if using  prototype pricechg property to replace price property . I want use the same price  property before and after the formula change. How to do it on javascript prototype ?
The following code is not working at the console.log("Bob pay new price:", guest_Bob_price.price(2,3));

please advise

Rwniceing

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<title>Title</title>

<script type="text/javascript">

function CookMenu(food,drink){
	//console.log(CookMenu.prototype.pricechg);//undefined
	if (CookMenu.prototype.pricechg==undefined){
	//this.price=100*food+200*drink;
    return this;
	}
} 
function start(){
var guest_Bob_price = new CookMenu(2,3);
var guest_John_price = new CookMenu(5,6);

console.log("Bob pay old price:", guest_Bob_price.price);
console.log("John pay old price:", guest_John_price.price);

//-Menu price might change if input value=1
if (document.getElementById("owner").value==1){
CookMenu.prototype={price:function(food,drink){ return 300*food+700*drink; }};
var owner_restaurant=new CookMenu(2,3);
}

var guest_Bob_price = new CookMenu(2,3);
var guest_John_price = new CookMenu(2,3);
console.log("Bob pay new price:", guest_Bob_price.price(2,3));
console.log("John pay new price:", guest_John_price.price(2,3));
}

</script>
<body onload="start();">
<div>
Prototype javascript tutorial, for example, restaurant menu price change
</div>
<div>
<input type="text" name="owner" id="owner" value="1" />
</div>
</body>
</html>

Open in new window

Comment
Watch Question
Owner (Aidellio)
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 32 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE