<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns ="http://www.w3.org/1999/xhtml" >
<head >
<meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
<title > 无标题文档</title >
</head >
<body >
<h1 > 产品最终价格:</h1 >
<div id ='type' > </div >
<div id ='mydiv' > </div >
</body >
</html >
<script >
var Price={};
Price.vipPrice = function () {
this .discount = 0.5 ;
this .getPrice = function (price) {
return price * this .discount;
}
}
Price.oldPrice = function () {
this .discount = 0.3 ;
this .getPrice = function (price)
{
return price * this .discount;
}
}
Price.NormlPrice = function () {
this .discount = 1 ;
this .getPrice = function (price)
{
return price ;
}
}
function Context () {
this .name = '' ;
this .strategy = null ;
this .price = 0 ;
}
Context.prototype.set = function (name, strategy, price) {
this .name = name;
this .strategy = strategy;
this .price = price;
}
Context.prototype.getResult = function () {
document.getElementById('type' ).innerHTML=this .name;;
document.getElementById('mydiv' ).innerHTML=this .strategy.getPrice(this .price);;
alert(this .strategy.getPrice(this .price));
}
var context = new Context();
var vip = new Price.vipPrice();
context.set ('vip客户' , vip, 200 );
context.getResult();
var old = new Price.oldPrice();
context.set ('老客户' , old, 200 );
context.getResult();
var vipPrice = function (price) {
return price * 0.5 ;
}
var oldPrice = function (price) {
return price * 0.3 ;
}
var NormlPrice = function (price) {
return price ;
}
var calculatePrice = function ( func, price ) {
return func(price);
};
var price = calculatePrice(vipPrice,1000 );
document.getElementById('mydiv' ).innerHTML=price;
</script >