console
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const calcPrice = (function () {
const sale = {
'100_10': function (price) { return price -= 10 },
'200_25': function (price) { return price -= 25 },
'80%': function (price) { return price *= 0.8 }
}
function calcPrice(price, type) {
if (!sale[type]) return '没有这个折扣'
return sale[type](price)
}
calcPrice.add = function (type, fn) {
if (sale[type]) return '该折扣已经存在'
sale[type] = fn
return '添加成功'
}
calcPrice.del = function (type) {
delete sale[type]
}
return calcPrice
})();
calcPrice.add('70%', function (price) { return price *= 0.7 })
const res = calcPrice(320, '70%')
console.dir(res)
calcPrice.del('100_10')
const res2 = calcPrice(320, '100_10')
console.log(res2)
</script>
</body>
</html>