Jump to content

what ? Para que são estes parentesis?


Gurzi

Recommended Posts

<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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.