2013-04-28 50 views
1

我確定這是一個非常基本的問題,但我剛剛開始學習JavaScript(過去總是使用PHP),並不能完全解決問題。我有基於用戶輸入執行計算的代碼,當jquery滑塊移動,文本框值改變或按下按鈕時結果更新 - 所有三個觸發器都會觸發相同的代碼,但我必須重複代碼三次,因爲它是如何被觸發的 - 有沒有辦法將它寫成一個代碼塊,而不是單獨的onchange,onclick和內置到滑塊查詢?對於以下長碼預先道歉...如何結合重複的javascript

$(document).ready(function() { 

$("#slider").slider({ 
    range: false, 
    min: 10000, 
    max: 200000, 
    value: 50000, 
    step: 1000, 
    slide: function(event, ui) { 
    $("#amount").val("$" + numberWithCommas(ui.value)); 
    }, 
change: function(event, ui) { 


var maxcap; 

var newValue = $("#slider").slider("value") ; 
var adult_prem=450; 
var child_prem=293; 
var prem; 

var ad = document.getElementById("famadult"); 
var adulthh = ad.options[ad.selectedIndex].value; 
var ch = document.getElementById("famchild"); 
var childhh = ch.options[ch.selectedIndex].value; 


var age=new Array(); 
age[0] = document.getElementById('adult_age').value; 
if (adulthh == 2) { 
age[1] = document.getElementById('adult2_age').value; 
q = 2; 
} 
else {q = 1;} 
var agerating=new Array(); 
var ageprem = 0; 
for (var i=0;i<q;i++) 
{ 

if (age[i] >=0 && age[i] <=20) {agerate = 0.635;} 
else if (age[i] > 20 && age[i] <= 24) {agerate = 1;} 
else if (age[i] == 25) {agerate = 1.004;} 
else if (age[i] == 26) {agerate = 1.024;} 
else if (age[i] == 27) {agerate = 1.048;} 
else if (age[i] == 28) {agerate = 1.087;} 
else if (age[i] == 29) {agerate = 1.119;} 
else if (age[i] == 30) {agerate = 1.135;} 
else if (age[i] == 31) {agerate = 1.159;} 
else if (age[i] == 32) {agerate = 1.183;} 
else if (age[i] == 33) {agerate = 1.198;} 
else if (age[i] == 34) {agerate = 1.214;} 
else if (age[i] == 35) {agerate = 1.222;} 
else if (age[i] == 36) {agerate = 1.230;} 
else if (age[i] == 37) {agerate = 1.238;} 
else if (age[i] == 38) {agerate = 1.246;} 
else if (age[i] == 39) {agerate = 1.262;} 
else if (age[i] == 40) {agerate = 1.278;} 
else if (age[i] == 41) {agerate = 1.302;} 
else if (age[i] == 42) {agerate = 1.325;} 
else if (age[i] == 43) {agerate = 1.357;} 
else if (age[i] == 44) {agerate = 1.397;} 
else if (age[i] == 45) {agerate = 1.444;} 
else if (age[i] == 46) {agerate = 1.5;} 
else if (age[i] == 47) {agerate = 1.563;} 
else if (age[i] == 48) {agerate = 1.635;} 
else if (age[i] == 49) {agerate = 1.706;} 
else if (age[i] == 50) {agerate = 1.786;} 
else if (age[i] == 51) {agerate = 1.865;} 
else if (age[i] == 52) {agerate = 1.952;} 
else if (age[i] == 53) {agerate = 2.040;} 
else if (age[i] == 54) {agerate = 2.135;} 
else if (age[i] == 55) {agerate = 2.230;} 
else if (age[i] == 56) {agerate = 2.333;} 
else if (age[i] == 57) {agerate = 2.437;} 
else if (age[i] == 58) {agerate = 2.548;} 
else if (age[i] == 59) {agerate = 2.603;} 
else if (age[i] == 60) {agerate = 2.714;} 
else if (age[i] == 61) {agerate = 2.810;} 
else if (age[i] == 62) {agerate = 2.873;} 
else if (age[i] == 63) {agerate = 2.952;} 
else if (age >= 64) {agerate = 3;} 
ageprem += agerate*adult_prem; 
agerating[i]=agerate; 
} 

if (adulthh == 2) { 
$('#spouse_div').show(); 
} 
else { 
$('#spouse_div').hide(); 
} 

prem = ageprem+(child_prem*childhh); 
prem = Math.round(prem); 
var fpl1=117.31; 
var fpl2=40.2; 
var fpl0; 

var fpl3 = ((adulthh-1)*fpl2)+(childhh*fpl2); 
fpl0 = fpl1+fpl3; 

var newFPL = Math.round(newValue/fpl0); 
var avfull = prem/0.7; 
if (newFPL >= 0 && newFPL < 130) {maxcap = 0.02;} 
else if (newFPL >= 130 && newFPL < 140) {maxcap = 0.03;} 
else if (newFPL >= 140 && newFPL < 150) {maxcap = 0.035;} 
else if (newFPL >= 150 && newFPL < 160) {maxcap = 0.04;} 
else if (newFPL >= 160 && newFPL < 170) {maxcap = 0.044;} 
else if (newFPL >= 170 && newFPL < 175) {maxcap = 0.048;} 
else if (newFPL >= 175 && newFPL < 180) {maxcap = 0.052;} 
else if (newFPL >= 180 && newFPL < 190) {maxcap = 0.055;} 
else if (newFPL >= 190 && newFPL < 200) {maxcap = 0.059;} 
else if (newFPL >= 200 && newFPL < 210) {maxcap = 0.063;} 
else if (newFPL >= 210 && newFPL < 220) {maxcap = 0.066;} 
else if (newFPL >= 220 && newFPL < 225) {maxcap = 0.069;} 
else if (newFPL >= 225 && newFPL < 230) {maxcap = 0.072;} 
else if (newFPL >= 230 && newFPL < 240) {maxcap = 0.075;} 
else if (newFPL >= 240 && newFPL < 250) {maxcap = 0.078;} 
else if (newFPL >= 250 && newFPL < 260) {maxcap = 0.081;} 
else if (newFPL >= 260 && newFPL < 270) {maxcap = 0.083;} 
else if (newFPL >= 270 && newFPL < 275) {maxcap = 0.085;} 
else if (newFPL >= 275 && newFPL < 280) {maxcap = 0.088;} 
else if (newFPL >= 280 && newFPL < 290) {maxcap = 0.09;} 
else if (newFPL >= 290 && newFPL < 300) {maxcap = 0.093;} 
else if (newFPL >= 300 && newFPL < 401) {maxcap = 0.095;} 
else {maxcap = 0;} 


var conscost; 
var cap; 
if (newFPL > 400) {cap=0;} 
else {cap = prem-Math.round((newValue*maxcap)/12);} 

if (newFPL < 100) {premsub = 0;} 
else if (newFPL >= 100 && newFPL < 151) {premsub = 0.24;} 
else if (newFPL >= 151 && newFPL < 201) {premsub = 0.17;} 
else if (newFPL >= 200 && newFPL < 251) {premsub = 0.03;} 
else {premsub = 0;} 
conscost = Math.max((prem-cap),0); 

function numberWithCommas(x) { 
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
} 
var pctincome = ((conscost*12)/newValue)*100; 
pctincome = pctincome.toFixed(1); 

document.getElementById("govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avful)); 
document.getElementById("max_cap").innerHTML=Math.round((newValue*maxcap)/12); 
document.getElementById("fpl_level").innerHTML=Math.round(newValue/fpl0); 
document.getElementById("mo_ins_prem_sub").innerHTML=numberWithCommas(cap); 
document.getElementById("mo_ins_prem").innerHTML=numberWithCommas(prem); 
document.getElementById("ann_govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull*12)); 
document.getElementById("ann_max_cap").innerHTML=numberWithCommas(Math.round(newValue*maxcap)); 
document.getElementById("ann_ins_prem").innerHTML=numberWithCommas(prem*12); 
document.getElementById("ann_ins_prem_sub").innerHTML=numberWithCommas(cap*12); 

document.getElementById("mo_cost").innerHTML=numberWithCommas(Math.round(conscost)); 
    document.getElementById("ann_cost").innerHTML=numberWithCommas(Math.round(conscost*12)); 
document.getElementById("ann_ins_prem_sub_pct").innerHTML=Math.round((1-(cap/prem))*100); 
document.getElementById("ann_ins_cap_pct").innerHTML=Math.round((cap/prem)*100); 
document.getElementById("age_factor").innerHTML=agerating[0]; 
document.getElementById("ann_ins_cap").innerHTML=pctincome; 
} 
}); 


function numberWithCommas(x) { 
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
} 

$("#amount").val("$" + numberWithCommas($("#slider").slider("value"))); 



$('#submit_subsidy').click(function() { 
    var maxcap; 
$('#subsidy_results').show(); 
var newValue = $("#slider").slider("value") ; 
var adult_prem=450; 
var child_prem=293; 
var prem; 

var ad = document.getElementById("famadult"); 
var adulthh = ad.options[ad.selectedIndex].value; 
var ch = document.getElementById("famchild"); 
var childhh = ch.options[ch.selectedIndex].value; 

var age=new Array(); 
age[0] = document.getElementById('adult_age').value; 
if (adulthh == 2) { 
age[1] = document.getElementById('adult2_age').value; 
q = 2; 
} 
else {q = 1;} 
var agerating=new Array(); 
var ageprem = 0; 
for (var i=0;i<q;i++) 
{ 

if (age[i] >=0 && age[i] <=20) {agerate = 0.635;} 
else if (age[i] > 20 && age[i] <= 24) {agerate = 1;} 
else if (age[i] == 25) {agerate = 1.004;} 
else if (age[i] == 26) {agerate = 1.024;} 
else if (age[i] == 27) {agerate = 1.048;} 
else if (age[i] == 28) {agerate = 1.087;} 
else if (age[i] == 29) {agerate = 1.119;} 
else if (age[i] == 30) {agerate = 1.135;} 
else if (age[i] == 31) {agerate = 1.159;} 
else if (age[i] == 32) {agerate = 1.183;} 
else if (age[i] == 33) {agerate = 1.198;} 
else if (age[i] == 34) {agerate = 1.214;} 
else if (age[i] == 35) {agerate = 1.222;} 
else if (age[i] == 36) {agerate = 1.230;} 
else if (age[i] == 37) {agerate = 1.238;} 
else if (age[i] == 38) {agerate = 1.246;} 
else if (age[i] == 39) {agerate = 1.262;} 
else if (age[i] == 40) {agerate = 1.278;} 
else if (age[i] == 41) {agerate = 1.302;} 
else if (age[i] == 42) {agerate = 1.325;} 
else if (age[i] == 43) {agerate = 1.357;} 
else if (age[i] == 44) {agerate = 1.397;} 
else if (age[i] == 45) {agerate = 1.444;} 
else if (age[i] == 46) {agerate = 1.5;} 
else if (age[i] == 47) {agerate = 1.563;} 
else if (age[i] == 48) {agerate = 1.635;} 
else if (age[i] == 49) {agerate = 1.706;} 
else if (age[i] == 50) {agerate = 1.786;} 
else if (age[i] == 51) {agerate = 1.865;} 
else if (age[i] == 52) {agerate = 1.952;} 
else if (age[i] == 53) {agerate = 2.040;} 
else if (age[i] == 54) {agerate = 2.135;} 
else if (age[i] == 55) {agerate = 2.230;} 
else if (age[i] == 56) {agerate = 2.333;} 
else if (age[i] == 57) {agerate = 2.437;} 
else if (age[i] == 58) {agerate = 2.548;} 
else if (age[i] == 59) {agerate = 2.603;} 
else if (age[i] == 60) {agerate = 2.714;} 
else if (age[i] == 61) {agerate = 2.810;} 
else if (age[i] == 62) {agerate = 2.873;} 
else if (age[i] == 63) {agerate = 2.952;} 
else if (age >= 64) {agerate = 3;} 
ageprem += agerate*adult_prem; 
agerating[i]=agerate; 
} 

if (adulthh == 2) { 
$('#spouse_div').show(); 
} 
else { 
$('#spouse_div').hide(); 
} 

prem = ageprem+(child_prem*childhh); 
prem = Math.round(prem); 
var fpl1=117.31; 
var fpl2=40.2; 
var fpl0; 

var fpl3 = ((adulthh-1)*fpl2)+(childhh*fpl2); 
fpl0 = fpl1+fpl3; 

var newFPL = Math.round(newValue/fpl0); 
var avfull = prem/0.7; 
if (newFPL >= 0 && newFPL < 130) {maxcap = 0.02;} 
else if (newFPL >= 130 && newFPL < 140) {maxcap = 0.03;} 
else if (newFPL >= 140 && newFPL < 150) {maxcap = 0.035;} 
else if (newFPL >= 150 && newFPL < 160) {maxcap = 0.04;} 
else if (newFPL >= 160 && newFPL < 170) {maxcap = 0.044;} 
else if (newFPL >= 170 && newFPL < 175) {maxcap = 0.048;} 
else if (newFPL >= 175 && newFPL < 180) {maxcap = 0.052;} 
else if (newFPL >= 180 && newFPL < 190) {maxcap = 0.055;} 
else if (newFPL >= 190 && newFPL < 200) {maxcap = 0.059;} 
else if (newFPL >= 200 && newFPL < 210) {maxcap = 0.063;} 
else if (newFPL >= 210 && newFPL < 220) {maxcap = 0.066;} 
else if (newFPL >= 220 && newFPL < 225) {maxcap = 0.069;} 
else if (newFPL >= 225 && newFPL < 230) {maxcap = 0.072;} 
else if (newFPL >= 230 && newFPL < 240) {maxcap = 0.075;} 
else if (newFPL >= 240 && newFPL < 250) {maxcap = 0.078;} 
else if (newFPL >= 250 && newFPL < 260) {maxcap = 0.081;} 
else if (newFPL >= 260 && newFPL < 270) {maxcap = 0.083;} 
else if (newFPL >= 270 && newFPL < 275) {maxcap = 0.085;} 
else if (newFPL >= 275 && newFPL < 280) {maxcap = 0.088;} 
else if (newFPL >= 280 && newFPL < 290) {maxcap = 0.09;} 
else if (newFPL >= 290 && newFPL < 300) {maxcap = 0.093;} 
else if (newFPL >= 300 && newFPL < 401) {maxcap = 0.095;} 
else {maxcap = 0;} 



var conscost; 
var cap; 
if (newFPL > 400) {cap=0;} 
else {cap = prem-Math.round((newValue*maxcap)/12);} 

if (newFPL < 100) {premsub = 0;} 
else if (newFPL >= 100 && newFPL < 151) {premsub = 0.24;} 
else if (newFPL >= 151 && newFPL < 201) {premsub = 0.17;} 
else if (newFPL >= 200 && newFPL < 251) {premsub = 0.03;} 
else {premsub = 0;} 
conscost = Math.max((prem-cap),0); 
function numberWithCommas(x) { 
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
} 
var pctincome = ((conscost*12)/newValue)*100; 
pctincome = pctincome.toFixed(1); 

document.getElementById("govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull)); 
document.getElementById("max_cap").innerHTML=Math.round((newValue*maxcap)/12); 
document.getElementById("fpl_level").innerHTML=Math.round(newValue/fpl0); 
document.getElementById("mo_ins_prem_sub").innerHTML=numberWithCommas(cap); 
document.getElementById("mo_ins_prem").innerHTML=numberWithCommas(prem); 
document.getElementById("ann_govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull*12)); 
document.getElementById("ann_max_cap").innerHTML=numberWithCommas(Math.round(newValue*maxcap)); 
document.getElementById("ann_ins_prem").innerHTML=numberWithCommas(prem*12); 
document.getElementById("ann_ins_prem_sub").innerHTML=numberWithCommas(cap*12); 
document.getElementById("mo_cost").innerHTML=numberWithCommas(Math.round(conscost)); 
document.getElementById("ann_cost").innerHTML=numberWithCommas(Math.round(conscost*12)); 
document.getElementById("ann_ins_prem_sub_pct").innerHTML=Math.round((1-(cap/prem))*100); 
document.getElementById("ann_ins_cap_pct").innerHTML=Math.round((cap/prem)*100); 
document.getElementById("age_factor").innerHTML=agerating[0]; 
document.getElementById("ann_ins_cap").innerHTML=pctincome; 
}); 


$('.sub_change').change(function() { 
    var maxcap; 

var newValue = $("#slider").slider("value") ; 
var adult_prem=450; 
var child_prem=293; 
var prem; 

var ad = document.getElementById("famadult"); 
var adulthh = ad.options[ad.selectedIndex].value; 
var ch = document.getElementById("famchild"); 
var childhh = ch.options[ch.selectedIndex].value; 

var age=new Array(); 
age[0] = document.getElementById('adult_age').value; 
if (adulthh == 2) { 
age[1] = document.getElementById('adult2_age').value; 
q = 2; 
} 
else {q = 1;} 
var agerating=new Array(); 
var ageprem = 0; 
for (var i=0;i<q;i++) 
{ 

if (age[i] >=0 && age[i] <=20) {agerate = 0.635;} 
else if (age[i] > 20 && age[i] <= 24) {agerate = 1;} 
else if (age[i] == 25) {agerate = 1.004;} 
else if (age[i] == 26) {agerate = 1.024;} 
else if (age[i] == 27) {agerate = 1.048;} 
else if (age[i] == 28) {agerate = 1.087;} 
else if (age[i] == 29) {agerate = 1.119;} 
else if (age[i] == 30) {agerate = 1.135;} 
else if (age[i] == 31) {agerate = 1.159;} 
else if (age[i] == 32) {agerate = 1.183;} 
else if (age[i] == 33) {agerate = 1.198;} 
else if (age[i] == 34) {agerate = 1.214;} 
else if (age[i] == 35) {agerate = 1.222;} 
else if (age[i] == 36) {agerate = 1.230;} 
else if (age[i] == 37) {agerate = 1.238;} 
else if (age[i] == 38) {agerate = 1.246;} 
else if (age[i] == 39) {agerate = 1.262;} 
else if (age[i] == 40) {agerate = 1.278;} 
else if (age[i] == 41) {agerate = 1.302;} 
else if (age[i] == 42) {agerate = 1.325;} 
else if (age[i] == 43) {agerate = 1.357;} 
else if (age[i] == 44) {agerate = 1.397;} 
else if (age[i] == 45) {agerate = 1.444;} 
else if (age[i] == 46) {agerate = 1.5;} 
else if (age[i] == 47) {agerate = 1.563;} 
else if (age[i] == 48) {agerate = 1.635;} 
else if (age[i] == 49) {agerate = 1.706;} 
else if (age[i] == 50) {agerate = 1.786;} 
else if (age[i] == 51) {agerate = 1.865;} 
else if (age[i] == 52) {agerate = 1.952;} 
else if (age[i] == 53) {agerate = 2.040;} 
else if (age[i] == 54) {agerate = 2.135;} 
else if (age[i] == 55) {agerate = 2.230;} 
else if (age[i] == 56) {agerate = 2.333;} 
else if (age[i] == 57) {agerate = 2.437;} 
else if (age[i] == 58) {agerate = 2.548;} 
else if (age[i] == 59) {agerate = 2.603;} 
else if (age[i] == 60) {agerate = 2.714;} 
else if (age[i] == 61) {agerate = 2.810;} 
else if (age[i] == 62) {agerate = 2.873;} 
else if (age[i] == 63) {agerate = 2.952;} 
else if (age >= 64) {agerate = 3;} 
ageprem += agerate*adult_prem; 
agerating[i]=agerate; 
} 

if (adulthh == 2) { 
$('#spouse_div').show(); 
} 
else { 
$('#spouse_div').hide(); 
} 

prem = ageprem+(child_prem*childhh); 
prem = Math.round(prem); 
var fpl1=117.31; 
var fpl2=40.2; 
var fpl0; 

var fpl3 = ((adulthh-1)*fpl2)+(childhh*fpl2); 
fpl0 = fpl1+fpl3; 

var newFPL = Math.round(newValue/fpl0); 
var avfull = prem/0.7; 
if (newFPL >= 0 && newFPL < 130) {maxcap = 0.02;} 
else if (newFPL >= 130 && newFPL < 140) {maxcap = 0.03;} 
else if (newFPL >= 140 && newFPL < 150) {maxcap = 0.035;} 
else if (newFPL >= 150 && newFPL < 160) {maxcap = 0.04;} 
else if (newFPL >= 160 && newFPL < 170) {maxcap = 0.044;} 
else if (newFPL >= 170 && newFPL < 175) {maxcap = 0.048;} 
else if (newFPL >= 175 && newFPL < 180) {maxcap = 0.052;} 
else if (newFPL >= 180 && newFPL < 190) {maxcap = 0.055;} 
else if (newFPL >= 190 && newFPL < 200) {maxcap = 0.059;} 
else if (newFPL >= 200 && newFPL < 210) {maxcap = 0.063;} 
else if (newFPL >= 210 && newFPL < 220) {maxcap = 0.066;} 
else if (newFPL >= 220 && newFPL < 225) {maxcap = 0.069;} 
else if (newFPL >= 225 && newFPL < 230) {maxcap = 0.072;} 
else if (newFPL >= 230 && newFPL < 240) {maxcap = 0.075;} 
else if (newFPL >= 240 && newFPL < 250) {maxcap = 0.078;} 
else if (newFPL >= 250 && newFPL < 260) {maxcap = 0.081;} 
else if (newFPL >= 260 && newFPL < 270) {maxcap = 0.083;} 
else if (newFPL >= 270 && newFPL < 275) {maxcap = 0.085;} 
else if (newFPL >= 275 && newFPL < 280) {maxcap = 0.088;} 
else if (newFPL >= 280 && newFPL < 290) {maxcap = 0.09;} 
else if (newFPL >= 290 && newFPL < 300) {maxcap = 0.093;} 
else if (newFPL >= 300 && newFPL < 401) {maxcap = 0.095;} 
else {maxcap = 0;} 


var conscost; 
var cap; 
if (newFPL > 400) {cap=0;} 
else {cap = prem-Math.round((newValue*maxcap)/12);} 

if (newFPL < 100) {premsub = 0;} 
else if (newFPL >= 100 && newFPL < 151) {premsub = 0.24;} 
else if (newFPL >= 151 && newFPL < 201) {premsub = 0.17;} 
else if (newFPL >= 200 && newFPL < 251) {premsub = 0.03;} 
else {premsub = 0;} 
conscost = Math.max((prem-cap),0); 
function numberWithCommas(x) { 
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
} 
var pctincome = ((conscost*12)/newValue)*100; 
pctincome = pctincome.toFixed(1); 

document.getElementById("govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull)); 
document.getElementById("max_cap").innerHTML=Math.round((newValue*maxcap)/12); 
document.getElementById("fpl_level").innerHTML=Math.round(newValue/fpl0); 
document.getElementById("mo_ins_prem_sub").innerHTML=numberWithCommas(cap); 
document.getElementById("mo_ins_prem").innerHTML=numberWithCommas(prem); 
document.getElementById("ann_govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull*12)); 
document.getElementById("ann_max_cap").innerHTML=numberWithCommas(Math.round(newValue*maxcap)); 
document.getElementById("ann_ins_prem").innerHTML=numberWithCommas(prem*12); 
document.getElementById("ann_ins_prem_sub").innerHTML=numberWithCommas(cap*12); 
document.getElementById("mo_cost").innerHTML=numberWithCommas(Math.round(conscost)); 
document.getElementById("ann_cost").innerHTML=numberWithCommas(Math.round(conscost*12)); 
document.getElementById("ann_ins_cap").innerHTML=pctincome; 
document.getElementById("ann_ins_prem_sub_pct").innerHTML=Math.round((1-(cap/prem))*100); 
document.getElementById("ann_ins_cap_pct").innerHTML=Math.round((cap/prem)*100); 

document.getElementById("age_factor").innerHTML=agerating[0]; 

}); 
}); 

感謝您的幫助! Mike

+3

如果你的問題是*我怎麼可以這樣寫更好的*,而不是*我怎麼能解決這個*? ,http://codereview.stackexchange.com是爲這個問題設計的網站:)。 – Matt 2013-04-28 18:53:10

+0

馬特 - 欣賞洞察力,不知道有codereview部分!一旦我將代碼拿到了我喜歡的地方,我會試試看。 – mvanmeter 2013-04-30 20:15:02

+0

這個問題似乎是無關緊要的,因爲它涉及代碼審查。 – g00glen00b 2014-03-02 12:23:17

回答

0

所以,你爲什麼不使用函數? 像

var myApp={}; 
myApp.complicatedCalculation=function(parameter){/* YaddaYadda */} 

所以每次,你需要它,你可以撥打:

myApp.complicatedCalculation(parameter) 
+0

謝謝莉莉絲 - 那有效。只是想知道爲什麼你使用'var myApp = {};'和'myApp.complicatedCalculation = function(parameter){} ...'不能像你說的那樣簡單'var myApp = function(parameter){/ * yaddayadda * /}'或者我錯過了什麼? – mvanmeter 2013-04-30 20:15:44

+0

不客氣。此外:使用變量myApp,它被初始化爲一個對象,然後用函數擴展是JS中的一種常見模式;它是JS的一種命名空間。因此,用於「myApp」的所有內容都位於此名稱空間下。更多情況下,您會看到使用var myApp = myApp ||進行初始化{},這意味着沒有別的:如果myApp被定義使用它;如果不是,這裏是定義。 – 2013-04-30 21:59:41