JavaScript - 创建一个简单的函数

时间:2014-12-07 20:53:34

标签: javascript

我在创建一个应该有两个参数的函数时遇到了麻烦

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

参数函数应为:sincostancotginvFs ..应为:acsin,{ {1}} ..

所以函数应该有这些东西。有人可以帮我吗?

1 个答案:

答案 0 :(得分: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
    }
}
相关问题