Last active
August 29, 2015 14:15
-
-
Save jamc92/317d18ccc78db80ce71d to your computer and use it in GitHub Desktop.
JS - Function as a return value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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