Funciones clausura
Son funciones que reornan una funcion con variables de un scope externo.
function testClosure(){
var x =4; // solo es local esta variable
return x;
}
///// Es este ejemplo la funcion interior puede accesar a las variables de la funcion exterior ya que las toma como variables globales, no es necesario pasarla como parametro.
function testClosure(){
var x =4; // solo es local esta variable
function closeX(){
return x;
}
retun closeX;
}
var checkloalX = testClosure();
checkLocalX();
---> 4
function buildCoverTicketMaker( transport )
{
return function (name) {
alert ("Here is your transportation ticket via the " + transport + ".\n" + "wlecome to the Cold Closures Cove" + name + "!");
}
}
var getSubmarineTicket = buildCoverTicketMaker( "Submarine");
var getBatleShipTicket = buildCoverTicketMaker( "Balteship");
var getGiantTicket = buildCoverGiantMaker( "Giant Seagull");
getSubmarineTicket ("cesar");
No hay comentarios:
Publicar un comentario