Javascript-how to run the corresponding function based on the variable content?

Source: Internet
Author: User
Tags define function
I have written several functions funA (x), funB (x), funC (x), funD (x), funE (x), funF (x ), I have a requirement to implement different calculations based on a specific parameter 'str. For example, when str 'ABC' is executed, it can execute x → funA (x) → funB (x) → funC (x) → y. when str 'AC' is executed, it can execute... I have written several functions funA (x), funB (x), funC (x), funD (x), funE (x), funF (x ), I have a requirement, according to a specific parameter StrTo implement different computations. For example, when Str = 'abc'You can run x → funA (x) → funB (x) → funC (x) → y. when str 'acs' is run, you can run x → funA (x) → funC (x) → funE (x) → y.

function fun_abc(x){    var a = funA(x);    var b = funB(a);    var c = funC(b);    var y = c;        return y;}

I know that you can write a switch based on str for case and jump to different fun_XXX (), but this is not flexible. The final goal is to have an array with different function names written in order. input x during the call, and the function corresponding to the first element in the array will run until the last function, output final result y.

Is there any other way to simplify it? Or can it be freely combined. for example, if a function of the same name is directly executed based on a variable like funstr = 'abc', the function abc (x) is executed when a function similar to funstr (x) is run )?

JavaScript or php syntax.

Reply content:

I have written several functions funA (x), funB (x), funC (x), funD (x), funE (x), funF (x ), I have a requirement, according to a specific parameterStrTo implement different computations. For example, whenStr = 'abc'You can run x → funA (x) → funB (x) → funC (x) → y. when str 'acs' is run, you can run x → funA (x) → funC (x) → funE (x) → y.

function fun_abc(x){    var a = funA(x);    var b = funB(a);    var c = funC(b);    var y = c;        return y;}

I know that you can write a switch based on str for case and jump to different fun_XXX (), but this is not flexible. The final goal is to have an array with different function names written in order. input x during the call, and the function corresponding to the first element in the array will run until the last function, output final result y.

Is there any other way to simplify it? Or can it be freely combined. for example, if a function of the same name is directly executed based on a variable like funstr = 'abc', the function abc (x) is executed when a function similar to funstr (x) is run )?

JavaScript or php syntax.

Let me give you an example. eval is too rough... Look at me.

function run(str, param) {  let handle = { a: a, b: b, c: c, d: d };  return str.split('').reduce((res, name) => handle[name](res), param);  function a(x) {    return x + '1';  }  function b(x) {    return x + '2';  }  function c(x) {    return x + '3';  }  function d(x) {    return x + '4';  }}run('acb',0); // "0132"

For example, similar ideas can be freely used.

@ Improvement on the basis of a fish that does not like tomatoes

  • Each callrunWhen using a function, four functions and a map are generated to compare the cost. Can be mergedrunOutside.

  • UsereduceReplaceforEach

Const funcTable = {a, B, c, d} function run (str, initialVal) {return str. split (''). reduce (result, funcName) => funcTable [funcName] (result), initialVal)} // define function a, function B...

Simplest, eval
Trouble point {'ABC': fabc}

The idea is simple,phpAndnodeI will post a sample code.

phpVersion

Function funstr ($ str, $ param) {$ len = strlen ($ str); for ($ I = 0; $ I <$ len; $ I ++) {$ fun = 'fun '. strtoupper (substr ($ str, $ I, 1); if (function_exists ($ fun) {$ param = $ fun ($ param) ;}} return $ param ;} function funA ($ x) {return $ x + 1;} function funB ($ x) {return $ x + 2;} echo funstr ('AB', 0 ); // output 3

nodeVersion

Function funstr (str, x) {var len = str. length; for (var I = 0; I
  

Put all the defined functions in an object, and set the return value to call the function;
Var AB = {

A: function (x) {console. log (x); return 1}, B: function (x) {console. log (x); return 2}, c: function (x) {console. log (x); return 3}, d: function (x) {console. log (x); return 4},} function run (str, x) {// str is the defined sequence, such as acb; x is the parameter var B = x; var aa = str. split ("") aa. forEach (function (x) {AB [x] (B); B = AB [x] (B);}) return B}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.