Skip to content

Instantly share code, notes, and snippets.

@jamc92
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamc92/317d18ccc78db80ce71d to your computer and use it in GitHub Desktop.
Save jamc92/317d18ccc78db80ce71d to your computer and use it in GitHub Desktop.
JS - Function as a return value
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Fuction as a Return</title>
</head>
<body>
<script src="returnFunction.js"></script>
</body>
</html>
/**
* Created by madrid on 16/02/15.
*/
//Si no existe la variable, crear el arreglo vacio
if (!NSLambdas) {
var NSLambdas = {};
}
//Se crea la variable junto a la notacion de objeto que tiene asignada una funcion
NSLambdas.filterFactory = function() {
//se crea un arreglo vacio
var that = {};
//el arreglo vacio apunta al objeto odd la cual tiene una funcion que se le pasa por parametro un elemento y retorna
//el modulus del elemento
that.odd = function(element) {
return element % 2;
};
//el arreglo vacio apunta al objeto even que tiene una funcion para retornar los elementos false(!) del elemento
//pasado por parametro
that.even = function(element) {
return !that.odd(element);
};
//retorna el arreglo con los elementos odd y even dentro del arreglo
return that;
};
//Se crea la funcion
(function() {
//Al cargar mostrar la ventana del navegador se apunta al evento de escucha al momento de cargar el JS y tiene una
//funcion por parametro que asigna a 3 variables un valor
window.addEventListener("load", function() {
//Se asigna un arreglo a items
var items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
//se asigna a la variable lo que retorna la funcion del arreglo
var factory = NSLambdas.filterFactory();
//se asigna a la variable el arreglo, filtrando con la variable factory y llamando al objeto even
var even = items.filter(factory.even);
//Se imprime el objeto even que contiene [2, 4, 6, 8, 10] con el valor modulus 0
console.log(even);
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment