{"action":"created","issue":{"url":"https://api.github.com/repos/Fictizia/Curso-JS-para-desarrolladores-web_ed6/issues/9","repository_url":"https://api.github.com/repos/Fictizia/Curso-JS-para-desarrolladores-web_ed6","labels_url":"https://api.github.com/repos/Fictizia/Curso-JS-para-desarrolladores-web_ed6/issues/9/labels{/name}","comments_url":"https://api.github.com/repos/Fictizia/Curso-JS-para-desarrolladores-web_ed6/issues/9/comments","events_url":"https://api.github.com/repos/Fictizia/Curso-JS-para-desarrolladores-web_ed6/issues/9/events","html_url":"https://github.com/Fictizia/Curso-JS-para-desarrolladores-web_ed6/issues/9","id":167247379,"number":9,"title":"return en funciones anidadas","user":{"login":"jgarciaruiz","id":5923852,"avatar_url":"https://avatars.githubusercontent.com/u/5923852?v=3","gravatar_id":"","url":"https://api.github.com/users/jgarciaruiz","html_url":"https://github.com/jgarciaruiz","followers_url":"https://api.github.com/users/jgarciaruiz/followers","following_url":"https://api.github.com/users/jgarciaruiz/following{/other_user}","gists_url":"https://api.github.com/users/jgarciaruiz/gists{/gist_id}","starred_url":"https://api.github.com/users/jgarciaruiz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jgarciaruiz/subscriptions","organizations_url":"https://api.github.com/users/jgarciaruiz/orgs","repos_url":"https://api.github.com/users/jgarciaruiz/repos","events_url":"https://api.github.com/users/jgarciaruiz/events{/privacy}","received_events_url":"https://api.github.com/users/jgarciaruiz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-07-24T18:45:22Z","updated_at":"2016-07-24T20:47:10Z","closed_at":null,"body":"Una duda que me surge, si necesito pasar los return de funciones anidadas a la función primaria, ¿esta forma sería la correcta o habría alguna otra mejor, por ejemplo trabajarlo con una estructura con methods? \r\n```\r\nfunction lorem(){\r\n\tvar qux;\r\n\tfunction subfunctionOne(smth){\r\n\t\tvar foo = smth;\r\n\t\treturn foo;\r\n\t}\r\n\tfunction subfunctionTwo(){\r\n\t\tvar bar = 2;\r\n\t\tvar yaz = subfunctionOne();\r\n\t\treturn yaz * bar;\r\n\t}\r\n\tvar a = subfunctionOne();\r\n\tvar b = subfunctionTwo(a);\r\n\r\n\tqux = a+b;\r\n\treturn qux;\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/Fictizia/Curso-JS-para-desarrolladores-web_ed6/issues/comments/234801268","html_url":"https://github.com/Fictizia/Curso-JS-para-desarrolladores-web_ed6/issues/9#issuecomment-234801268","issue_url":"https://api.github.com/repos/Fictizia/Curso-JS-para-desarrolladores-web_ed6/issues/9","id":234801268,"user":{"login":"UlisesGascon","id":5110813,"avatar_url":"https://avatars.githubusercontent.com/u/5110813?v=3","gravatar_id":"","url":"https://api.github.com/users/UlisesGascon","html_url":"https://github.com/UlisesGascon","followers_url":"https://api.github.com/users/UlisesGascon/followers","following_url":"https://api.github.com/users/UlisesGascon/following{/other_user}","gists_url":"https://api.github.com/users/UlisesGascon/gists{/gist_id}","starred_url":"https://api.github.com/users/UlisesGascon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/UlisesGascon/subscriptions","organizations_url":"https://api.github.com/users/UlisesGascon/orgs","repos_url":"https://api.github.com/users/UlisesGascon/repos","events_url":"https://api.github.com/users/UlisesGascon/events{/privacy}","received_events_url":"https://api.github.com/users/UlisesGascon/received_events","type":"User","site_admin":false},"created_at":"2016-07-24T20:47:10Z","updated_at":"2016-07-24T20:47:10Z","body":"Hola @jgarciaruiz ! Es una forma válida. Aunque la estructura  del ejemplo es compleja y puede optimizarse mucho, al final *lorem* retorna la suma de los retornos de *subfunctionOne* y *subfunctionTwo*.\r\n\r\nOtra forma un poco más refinada sería esta:\r\n\r\n```javascript\r\nfunction lorem(){\r\n\r\n    function subfunctionOne(smth){\r\n        return smth || 0; // Para evitar el NaN original\r\n    }\r\n    function subfunctionTwo(){\r\n        return subfunctionOne() * 2;\r\n    }\r\n    \r\n    var a = subfunctionOne();\r\n    return a + subfunctionTwo(a);\r\n}\r\n```\r\nAunque lo más común en los retornos suele ser presentar el retorno de las funciones, también podemos exponer las funciones como retorno, de tal forma que podemos crear estructuras mucho más dinámicas que podemos ejecutar con variaciones según cambien las necesidades de nuestro Script, lo que da pie a utilizar patrones de diseño como [Facade](https://www.wikiwand.com/es/Facade_(patr%C3%B3n_de_dise%C3%B1o)).\r\n\r\nOtro tema que puede ser interesante para reducir la anidación y retornos complejos es utilizar [Closures](https://developer.mozilla.org/es/docs/Web/JavaScript/Closures)\r\n\r\nSi quieres en la próxima clase podemos profundizar un poco más en los retornos de funciones y métodos. ;-) \r\n"}}
{"actor":{"display_login":"UlisesGascon"}}