[Prototype-core] Re: Javascript closures question
Hi Simon, It is very easy, just go to search about JavaScript Function Declaration and Function Expresion (which Jscript doesn't support), it is just function and scope, yes JS is an amazing language which support this kind of feature, so do not say anything like annonymous function, there is no annonymous function, just Function Declaration and Function Expression and the Expression which IE and Jscript doesn't support. On Jan 9, 10:02 am, Simon Thomas <[EMAIL PROTECTED]> wrote: > Ye sorry Mislav, > > Haven't found a decent Javascript discussion group/forum. > > Been playing around with Prototype recently, just figured this would > be a good place to ask. > > I don't use Rails myself, Php, Mysql and Javascript developer mainly. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Javascript closures question
Ye sorry Mislav, Haven't found a decent Javascript discussion group/forum. Been playing around with Prototype recently, just figured this would be a good place to ask. I don't use Rails myself, Php, Mysql and Javascript developer mainly. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Javascript closures question
Thanks Nicolas, As i thought, just was unsure if method 2 had any significance with the use of wrapping parenthesis.Obviously not, thanks for clearing that up for me. I found the Module pattern originally on Douglas Crockfords site and am playing around with it. Currently trying to get my head around using it to simulate static class patterns with private variables etc.. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Javascript closures question
On Jan 8, 2008 11:44 PM, Mislav Marohnić <[EMAIL PROTECTED]> wrote: > Nicolás shouldn't have answered your question because this group is for > discussion of development for Prototype core library. > > You can ask general questions on "rails-spinoffs" group or, if it's only > related to Core JavaScript, on any other JavaScript mailing list. > > Thanks! > > > > On Jan 9, 2008 2:36 AM, Nicolás Sanguinetti <[EMAIL PROTECTED]> wrote: > > > > > > On Jan 8, 2008 11:20 PM, Simon Thomas <[EMAIL PROTECTED]> wrote: > > > > > > Can anyone explain the difference in the folllowing and when and why > > > to use each of the methods. > > > (Or knows a better place to ask this question) > > > > > > 1) > > > var myVar = function(args){ > > >// inner functions and vars > > > }; > > > > This makes myVar point to that function, so you can later call > > myVar(args), it's a normal function declaration and it's almost > > equivalent to > > doing function myVar(args) { ... }. As functions are first-order > > citizens in JavaScript, you can just assign them to a variable. > > > > > > > 2) > > > var myVar = (function(args){ > > >// inner functions and vars > > > }); > > > > This is exactly the same as (1). As any object in JS, a function can > > be wrapped in parenthesis with no consequence. > > > > > > > 3) > > > var myVar = (function(args){ > > >return { > > >//inner functions and vars > > > } > > > })(); > > > > This will declare an anonymous function that receives "args" (only one > > argument), and *immediately after declaring it* it will call it > > passing *no arguments*. So if you depend on args inside the function > > that will error out. Since it executes it, myVar will point to > > whatever the function returns. > > > > This technique was called "The Module Pattern" by Douglas Crockford, > > and has been described throughly in the interweb: > > - http://yuiblog.com/blog/2007/06/12/module-pattern/ > > - http://wait-till-i.com/2007/07/24/show-love-to-the-module-pattern/ > > - > http://nefariousdesigns.co.uk/archive/2007/08/javascript-module-pattern-variations/ > > > > Best, > > -Nicolas > > > > > > > > > > > > > > > > > > > > > > Oops, hadn't noticed this was in Proto-core. Sorry, I never notice if a thread is in core or spinoffs :-\ -N --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Javascript closures question
Nicolás shouldn't have answered your question because this group is for discussion of development for Prototype core library. You can ask general questions on "rails-spinoffs" group or, if it's only related to Core JavaScript, on any other JavaScript mailing list. Thanks! On Jan 9, 2008 2:36 AM, Nicolás Sanguinetti <[EMAIL PROTECTED]> wrote: > > On Jan 8, 2008 11:20 PM, Simon Thomas <[EMAIL PROTECTED]> wrote: > > > > Can anyone explain the difference in the folllowing and when and why > > to use each of the methods. > > (Or knows a better place to ask this question) > > > > 1) > > var myVar = function(args){ > >// inner functions and vars > > }; > > This makes myVar point to that function, so you can later call > myVar(args), it's a normal function declaration and it's almost > equivalent to > doing function myVar(args) { ... }. As functions are first-order > citizens in JavaScript, you can just assign them to a variable. > > > 2) > > var myVar = (function(args){ > >// inner functions and vars > > }); > > This is exactly the same as (1). As any object in JS, a function can > be wrapped in parenthesis with no consequence. > > > 3) > > var myVar = (function(args){ > >return { > >//inner functions and vars > > } > > })(); > > This will declare an anonymous function that receives "args" (only one > argument), and *immediately after declaring it* it will call it > passing *no arguments*. So if you depend on args inside the function > that will error out. Since it executes it, myVar will point to > whatever the function returns. > > This technique was called "The Module Pattern" by Douglas Crockford, > and has been described throughly in the interweb: > - http://yuiblog.com/blog/2007/06/12/module-pattern/ > - http://wait-till-i.com/2007/07/24/show-love-to-the-module-pattern/ > - > http://nefariousdesigns.co.uk/archive/2007/08/javascript-module-pattern-variations/ > > Best, > -Nicolas > > > > > > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Javascript closures question
On Jan 8, 2008 11:20 PM, Simon Thomas <[EMAIL PROTECTED]> wrote: > > Can anyone explain the difference in the folllowing and when and why > to use each of the methods. > (Or knows a better place to ask this question) > > 1) > var myVar = function(args){ >// inner functions and vars > }; This makes myVar point to that function, so you can later call myVar(args), it's a normal function declaration and it's almost equivalent to doing function myVar(args) { ... }. As functions are first-order citizens in JavaScript, you can just assign them to a variable. > 2) > var myVar = (function(args){ >// inner functions and vars > }); This is exactly the same as (1). As any object in JS, a function can be wrapped in parenthesis with no consequence. > 3) > var myVar = (function(args){ >return { >//inner functions and vars > } > })(); This will declare an anonymous function that receives "args" (only one argument), and *immediately after declaring it* it will call it passing *no arguments*. So if you depend on args inside the function that will error out. Since it executes it, myVar will point to whatever the function returns. This technique was called "The Module Pattern" by Douglas Crockford, and has been described throughly in the interweb: - http://yuiblog.com/blog/2007/06/12/module-pattern/ - http://wait-till-i.com/2007/07/24/show-love-to-the-module-pattern/ - http://nefariousdesigns.co.uk/archive/2007/08/javascript-module-pattern-variations/ Best, -Nicolas > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---
[Prototype-core] Re: Javascript closures question
PS for some reason this forum code keeps splitting the 3rd example into the 'Show Quoted Text' area grr :) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype: Core" group. To post to this group, send email to prototype-core@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-core?hl=en -~--~~~~--~~--~--~---