Gurzi Posted November 7, 2007 at 01:17 AM Report Share #145811 Posted November 7, 2007 at 01:17 AM <script type="text/javascript"> (function() { var fade = function() { YAHOO.util.Dom.setStyle('foo', 'opacity', 0.5); }; YAHOO.util.Event.on('demo-run', 'click', fade); YAHOO.log("The example has finished loading; as you interact with it, you'll see log messages appearing here.", "info", "example"); })(); </script> Alguem faz ideia para que é que a function está dentro de parentesis e o que são aqueles () no fim antes do </script> ?? Link to comment Share on other sites More sharing options...
djthyrax Posted November 7, 2007 at 01:35 AM Report Share #145814 Posted November 7, 2007 at 01:35 AM Isso é uma função anónima, uma característica bastante útil do JavaScript, evita que tenhas problemas de estar a redefinir funções. 🙂 Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum! Link to comment Share on other sites More sharing options...
Gurzi Posted November 7, 2007 at 03:35 PM Author Report Share #145906 Posted November 7, 2007 at 03:35 PM sim, mas qual é a finalidade ?? ok, eu até percebo que nunca mais a vás chamar a função, mas o que são aqueles () no fim ??? Link to comment Share on other sites More sharing options...
djthyrax Posted November 7, 2007 at 03:54 PM Report Share #145910 Posted November 7, 2007 at 03:54 PM São para executar... Esta parte: (function() { var fade = function() { YAHOO.util.Dom.setStyle('foo', 'opacity', 0.5); }; YAHOO.util.Event.on('demo-run', 'click', fade); YAHOO.log("The example has finished loading; as you interact with it, you'll see log messages appearing here.", "info", "example"); }) devolve uma função anónima. Para a chamares, é só pores os (); à frente... É o mesmo que atrabuires essa parte a uma variável e depois fazeres variavel(); 🙂 A finalidade disto é "imitar" os lambda's de Python e de todas as linguagens funcionais. Usa-se essencialmente para poderes redefinires funções noutro ficheiro. Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now