《大话设计模式》用JavaScript语言描述(二)
第一章1.8节的源代码重写:
Code
var Operation = {
GetResult: function(numA, numB, operate) {
var result = 0.00;
switch(operate) {
case '+':
result = numA + numB;
break;
case '-':
result = numA - numB;
break;
case '*':
result = numA * numB;
break;
case '/':
result = numA / numB;
break;
}
return result;
}
}
var Operation = {
GetResult: function(numA, numB, operate) {
var result = 0.00;
switch(operate) {
case '+':
result = numA + numB;
break;
case '-':
result = numA - numB;
break;
case '*':
result = numA * numB;
break;
case '/':
result = numA / numB;
break;
}
return result;
}
}
程序部分:
Code
Program = {
main: function() {
if(!document.getElementById) return;
try {
var sNumA = document.getElementById('numA').value;
var sOperate = document.getElementById('operate').value;
var sNumB = document.getElementById('numB').value;
var sResult = '';
sResult = (Operation.GetResult(Number(sNumA), Number(sNumB), sOperate)).toString();
document.getElementById('compute').onclick = function() {
return document.getElementById('result').innerHTML = sResult;
}
}
catch(e) {
console.log('你的输入有误:' + e);
}
}
}
Program = {
main: function() {
if(!document.getElementById) return;
try {
var sNumA = document.getElementById('numA').value;
var sOperate = document.getElementById('operate').value;
var sNumB = document.getElementById('numB').value;
var sResult = '';
sResult = (Operation.GetResult(Number(sNumA), Number(sNumB), sOperate)).toString();
document.getElementById('compute').onclick = function() {
return document.getElementById('result').innerHTML = sResult;
}
}
catch(e) {
console.log('你的输入有误:' + e);
}
}
}