search

Home  >  Q&A  >  body text

javascript - When adding a new function to a function, should you choose to extend the original function or create a new function~

If you expand on the original function, you have to add parameters and if. This causes more judgment when using normal functions. Unhappy

If you create a new function, you will declare a piece of memory for it, which will consume more memory~

ringa_leeringa_lee2752 days ago522

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-05-19 10:25:44

    There is no best answer to this question. After all, you have to start from the actual project,
    And you should think and make choices from the perspectives of scalability/maintenance/readability, etc., these are more important.
    As for memory You are just worrying about consumption and the like. Even the largest js application at present will not be so scrupulous about the memory usage of a function...

    Well, let me add that if you insist on high performance, I have thought of a way:

    function foo( flag ) {
       foo = !!flag? function() {
           return true;
       }: function () {
           return false;
       }
       return foo();
    }
    foo(true);
    console.log( foo ); // 你可以看到最终的 foo 不再是一开始定义的样子

    The role of the function is not determined until the first time it is used, and all useless logic is removed after it is determined.
    Lazy loading + minimal memory usage.
    Theoretically, multiple calls should have the best performance.

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-19 10:25:44

    Functional programming vs. imperative programming

    reply
    0
  • Cancelreply