[Javascript] Intro to Recursion-refactoring to a Pure Function

Source: Internet
Author: User

Previous post:http://www.cnblogs.com/answer1215/p/4990418.html

= [' dist '= {  "dist": ["Build", "deploy"],  "Build": [' js ', ' css ', ' vender '],   "js": [' Babel ', ' ng-annotate ', "uglify"],  "css": ["Sass", "css-min"= []; Gettasks (input); function gettasks (input) {    Input.foreach (Task)={    if(Config[task]) {      gettasks (Config[task]);    } Else {      Tasks.push (Task);}}  )}; Console.log (tasks);

The Gettasks works but have some problem:

    • We depend on the outside variable ' tasks ' and ' config ', so there is side effect
    • There is no return value, hard to test
Let input, config, Tasks;input= [' Dist '];config= {  "Dist": ["Build", "deploy"],  "Build": [' js ', ' css ', ' vender '],  "JS": [' Babel ', ' ng-annotate ', ' uglify '],  "CSS": ["Sass", "Css-min"]};tasks= [];varres =gettasks (input, []);functiongettasks (input, initial) {returnInput.reduce (prev, next) ={    if(Config[next]) {returnGettasks (Config[next], prev); }Else{      returnPrev.concat (next); }}, initial);}; Console.log (res);

The code has been improved, we return the value from the Gettasks () function and we don ' t modify the tasks array anymore.

Just One thing we still need to do are we still depend on ' config ':

Let input, Config;input= [' Dist '];config= {  "Dist": ["Build", "deploy"],  "Build": [' js ', ' css ', ' vender '],  "JS": [' Babel ', ' ng-annotate ', ' uglify '],  "CSS": ["Sass", "Css-min"]};varres =gettasks (config, input, []);functiongettasks (config, input, initial) {returnInput.reduce (prev, next) ={    if(Config[next]) {returngettasks (config, Config[next], prev); }Else{      returnPrev.concat (next); }}, initial);}; Console.log (res);

[Javascript] Intro to Recursion-refactoring to a Pure Function

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.