2014-12-07 96 views
-5

我無法創建一個功能,那應該有兩個參數的JavaScript - 創建一個簡單的功能

UzelFunkce : function(fn, invFn) { 
    //... 
} 

參數功能應該是:sincostancotginvFs ..應該是:acsinacos ..

因此函數應該有這些東西。任何人都可以幫助我嗎?

+0

所以哪裏是問題嗎? – Grundy 2014-12-07 20:57:24

+0

你應該使用:'UzelFunkce:功能(FN,invFn)'代替冗長的文字和保留字... – 2014-12-07 20:57:35

+0

好吧,我有改名功能FN,等來invFn,以及我需要的是,這個功能應該返回這個東西(sin cos ...) – Cappylol 2014-12-07 20:59:16

回答

1
// didn't really test this, but the ideas should be there 
function UzelFunkce(fn, fnInv) { 
    var fns = { 
     sin: function(x) return MATH.sin(x), 
     cos: function(x) return MATH.cos(x), 
     tan: function(x) return MATH.tan(x), 
     cot: function(x) return 1/MATH.tan(x) 
    }; 
    var fnInvs = { 
     arcSin: function(x) return MATH.asin(x), 
     arcCos: function(x) return MATH.acos(x), 
     arcTan: function(x) return MATH.atan(x), 
     arcCot: function(x) return MATH.atan(1/x) 
    }; 
    if (typeof fns[fn] !== 'undefined' 
    && typeof fnInvs[fnInv] !== 'undefined') { 
     // both fn and fnInverse are valid according to the object above 
     // and allows you to use fns[fn](x) and fnInvs[fnInv](x) 

     // or... 
     fn = fns[fn], fnInv = fnInvs[fnInv]; 

     // fn & fnInv are now valid functions based on the string input. 
     // I don't suggest editing the parameters/arguments unless you know what you are doing 

     // You should also add checks on input, making sure they are strings or w/e 
    } 
} 
+0

非常感謝你,你明白我的意思:-) – Cappylol 2014-12-07 21:29:41