javascript - When adding a new function to a function, should you choose to extend the original function or create a new function~
ringa_lee
ringa_lee 2017-05-19 10:23:44
0
2
518

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_lee
ringa_lee

ringa_lee

reply all(2)
小葫芦

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.

世界只因有你

Functional programming vs. imperative programming

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template