[Proto-Scripty] Re: evalScripts and functions
Hi ColinFine, It doesn't seem to me that these imply that there will be a different scope, but I'm not sure. I translate badlly my though (and I'm probably wrong). The created function in both case have the same scope (I think too). According to what you quote, I made the following test: var _f=function(){return true}; var F; if(_f()===true) F=function(){alert('F true');} else F=function(){alert('F false');}; F(); var _ff=true; if((function(){return false})()===true) { function FF(){alert('FF true');} } else { function FF(){alert('FF false');}; } FF(); and in both case it work as expected : first 'true' and second 'false'. Means you could use function in the if test, inside the if statement and both way of writting the function have the same result. - function statmenes are subject to 'hoisting'. This means that regardless of where a function is placed, it is moved to the top of the scope in which it is defined. ... It also prohibits the use of function statements in if statements. ... This part doesn't seems to me a bad things, it permit to define a function after it's usage (in the created code, not during execution of code of course). I've perhaps miss something ??? -- david On 26 mai, 14:06, ColinFine colin.f...@pace.com wrote: On May 26, 12:41 pm, david david.brill...@gmail.com wrote: @ColinFine, According to Flanagan's book (section 8.1.2) the optional function- name in a function literal is not assigned to a variable, but apparently lives in a special namespace of its own that allows the function to refer to itself. So unless the function is recursive, the above is identical to var myFunction = function() {alert('hi');} and more or less identical to function myFunction() {alert('hi')} I try to refer to myFunction recursivelly and in both case it works (test done on FF3.0). could you send me an exemple of what you say ? No I can't. I'm just quoting from the books. I'd only though the difference was in the scope of the created function depending on the way it is declare as a *statement* or as a *litteral*. I didn't go into the reasons why Crockford advises not using function statements. They are: - the second form makes it clear that [myFunction] is a variable containing a function value. To use the language well, it is important to understand that functions are values - function statmenes are subject to 'hoisting'. This means that regardless of where a function is placed, it is moved to the top of the scope in which it is defined. ... It also prohibits the use of function statements in if statements. ... It doesn't seem to me that these imply that there will be a different scope, but I'm not sure. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: evalScripts and functions
Hi again ColinFine, Just had another test that make me understand what you quote: var _f=function(){return true}; var F; if(_f()===true) F=function(){alert('F true');} else F=function(){alert('F false');}; F(); var _ff=true; if((function(){return false})()===true) { function FF(){alert('FF true');} } else { function FF(){alert('FF false');}; } FF(); function F(){alert('new F');} F(); F=function(){alert('last F');} F(); first alert return true, second false. For the next alert, received: 'F true' and the last one 'last F'. But it seems not abnormal, while assigning a function to a variable is done at execution time, and defining a statement functionis done at parse time. I think this is what was saying crokford in your quote. But that seems not abnormal, and even not EVIL ... but that's my poor opinion. -- david On 27 mai, 12:48, david david.brill...@gmail.com wrote: Hi ColinFine, It doesn't seem to me that these imply that there will be a different scope, but I'm not sure. I translate badlly my though (and I'm probably wrong). The created function in both case have the same scope (I think too). According to what you quote, I made the following test: var _f=function(){return true}; var F; if(_f()===true) F=function(){alert('F true');} else F=function(){alert('F false');}; F(); var _ff=true; if((function(){return false})()===true) { function FF(){alert('FF true');} } else { function FF(){alert('FF false');}; } FF(); and in both case it work as expected : first 'true' and second 'false'. Means you could use function in the if test, inside the if statement and both way of writting the function have the same result. - function statmenes are subject to 'hoisting'. This means that regardless of where a function is placed, it is moved to the top of the scope in which it is defined. ... It also prohibits the use of function statements in if statements. ... This part doesn't seems to me a bad things, it permit to define a function after it's usage (in the created code, not during execution of code of course). I've perhaps miss something ??? -- david On 26 mai, 14:06, ColinFine colin.f...@pace.com wrote: On May 26, 12:41 pm, david david.brill...@gmail.com wrote: @ColinFine, According to Flanagan's book (section 8.1.2) the optional function- name in a function literal is not assigned to a variable, but apparently lives in a special namespace of its own that allows the function to refer to itself. So unless the function is recursive, the above is identical to var myFunction = function() {alert('hi');} and more or less identical to function myFunction() {alert('hi')} I try to refer to myFunction recursivelly and in both case it works (test done on FF3.0). could you send me an exemple of what you say ? No I can't. I'm just quoting from the books. I'd only though the difference was in the scope of the created function depending on the way it is declare as a *statement* or as a *litteral*. I didn't go into the reasons why Crockford advises not using function statements. They are: - the second form makes it clear that [myFunction] is a variable containing a function value. To use the language well, it is important to understand that functions are values - function statmenes are subject to 'hoisting'. This means that regardless of where a function is placed, it is moved to the top of the scope in which it is defined. ... It also prohibits the use of function statements in if statements. ... It doesn't seem to me that these imply that there will be a different scope, but I'm not sure. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: evalScripts and functions
Hi David, Could you please point me out where it is written, I've perhaps miss something. It's cleverly hidden on the docs for Ajax.Updater: http://prototypejs.org/api/ajax/updater A better place would (obviously) be on String#evalScripts, which someone helpfullly pointed out by opening a ticket in Lighthouse a while back (https://prototype.lighthouseapp.com/projects/8886- prototype/tickets/38). HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On May 25, 6:25 pm, david david.brill...@gmail.com wrote: Hi Mickael, I just look at the prototype documentation, and did not find any note about: According to the Prototype documentation, you need to declare the function and assign it to a global variable: myFunction = function() {alert('hi');} Could you please point me out where it is written, I've perhaps miss something. Because for me, there is no much difference in JS between: var myFunction=function(){alert('myFunction');} and function myFunction(){alert('myFunction');} that should return a function named myFunction that in both case could be called as myFunction(). I think you could define your function the way you want even in an AJAX call, because it's just handle (in certain case) the call to the eval() method which will only evaluate the JS code. For you're exemple, I think that var myFunction = function myFunction() {alert('hi');} just create a function called myFunction and assign this function to a variable called myFunction, that result in a function called myFunction() == I think it is redondant. -- david On 24 mai, 16:24, Michael mich...@michaelminella.com wrote: I understand how Prototype works with regards to the removal of script tags after evaling the results of an Ajax request. However, I was doing some research and am now starting to wonder why the way I declare functions works. According to the Prototype documentation, you need to declare the function and assign it to a global variable: myFunction = function() {alert('hi');} That makes sense. However, in all of my scenarios, I've declared functions like this: var myFunction = function myFunction() {alert('hi');} and the calls to myFunction work just fine. My question is...why does my way work? According to the Prototype documentation, the local variable myFunction should be thrown away after the eval. Any insight anyone can provide would be appreciated. Thanks in advance! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: evalScripts and functions
On May 24, 3:24 pm, Michael mich...@michaelminella.com wrote: However, in all of my scenarios, I've declared functions like this: var myFunction = function myFunction() {alert('hi');} and the calls to myFunction work just fine. My question is...why does my way work? According to the Prototype documentation, the local variable myFunction should be thrown away after the eval. Any insight anyone can provide would be appreciated. Thanks in advance! According to Flanagan's book (section 8.1.2) the optional function- name in a function literal is not assigned to a variable, but apparently lives in a special namespace of its own that allows the function to refer to itself. So unless the function is recursive, the above is identical to var myFunction = function() {alert('hi');} and more or less identical to function myFunction() {alert('hi')} (which is a function *statement* not a function *literal*, and so has different syntax, with the name being required.) Crockford, in Javascript: The Good Parts describes function *statements* as one of the Bad Parts (appendix B), and recommends avoiding them, in favour of the var f = function() {} form. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: evalScripts and functions
@TJ, thanks for the links, as you say, it's cleverly hidden. Perhaps I'm not enough curious !! @ColinFine, According to Flanagan's book (section 8.1.2) the optional function- name in a function literal is not assigned to a variable, but apparently lives in a special namespace of its own that allows the function to refer to itself. So unless the function is recursive, the above is identical to var myFunction = function() {alert('hi');} and more or less identical to function myFunction() {alert('hi')} I try to refer to myFunction recursivelly and in both case it works (test done on FF3.0). could you send me an exemple of what you say ? I'd only though the difference was in the scope of the created function depending on the way it is declare as a *statement* or as a *litteral*. -- david On 26 mai, 12:53, ColinFine colin.f...@pace.com wrote: On May 24, 3:24 pm, Michael mich...@michaelminella.com wrote: However, in all of my scenarios, I've declared functions like this: var myFunction = function myFunction() {alert('hi');} and the calls to myFunction work just fine. My question is...why does my way work? According to the Prototype documentation, the local variable myFunction should be thrown away after the eval. Any insight anyone can provide would be appreciated. Thanks in advance! According to Flanagan's book (section 8.1.2) the optional function- name in a function literal is not assigned to a variable, but apparently lives in a special namespace of its own that allows the function to refer to itself. So unless the function is recursive, the above is identical to var myFunction = function() {alert('hi');} and more or less identical to function myFunction() {alert('hi')} (which is a function *statement* not a function *literal*, and so has different syntax, with the name being required.) Crockford, in Javascript: The Good Parts describes function *statements* as one of the Bad Parts (appendix B), and recommends avoiding them, in favour of the var f = function() {} form. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: evalScripts and functions
On May 26, 12:41 pm, david david.brill...@gmail.com wrote: @ColinFine, According to Flanagan's book (section 8.1.2) the optional function- name in a function literal is not assigned to a variable, but apparently lives in a special namespace of its own that allows the function to refer to itself. So unless the function is recursive, the above is identical to var myFunction = function() {alert('hi');} and more or less identical to function myFunction() {alert('hi')} I try to refer to myFunction recursivelly and in both case it works (test done on FF3.0). could you send me an exemple of what you say ? No I can't. I'm just quoting from the books. I'd only though the difference was in the scope of the created function depending on the way it is declare as a *statement* or as a *litteral*. I didn't go into the reasons why Crockford advises not using function statements. They are: - the second form makes it clear that [myFunction] is a variable containing a function value. To use the language well, it is important to understand that functions are values - function statmenes are subject to 'hoisting'. This means that regardless of where a function is placed, it is moved to the top of the scope in which it is defined. ... It also prohibits the use of function statements in if statements. ... It doesn't seem to me that these imply that there will be a different scope, but I'm not sure. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: evalScripts and functions
Hi Mickael, I just look at the prototype documentation, and did not find any note about: According to the Prototype documentation, you need to declare the function and assign it to a global variable: myFunction = function() {alert('hi');} Could you please point me out where it is written, I've perhaps miss something. Because for me, there is no much difference in JS between: var myFunction=function(){alert('myFunction');} and function myFunction(){alert('myFunction');} that should return a function named myFunction that in both case could be called as myFunction(). I think you could define your function the way you want even in an AJAX call, because it's just handle (in certain case) the call to the eval() method which will only evaluate the JS code. For you're exemple, I think that var myFunction = function myFunction() {alert('hi');} just create a function called myFunction and assign this function to a variable called myFunction, that result in a function called myFunction() == I think it is redondant. -- david On 24 mai, 16:24, Michael mich...@michaelminella.com wrote: I understand how Prototype works with regards to the removal of script tags after evaling the results of an Ajax request. However, I was doing some research and am now starting to wonder why the way I declare functions works. According to the Prototype documentation, you need to declare the function and assign it to a global variable: myFunction = function() {alert('hi');} That makes sense. However, in all of my scenarios, I've declared functions like this: var myFunction = function myFunction() {alert('hi');} and the calls to myFunction work just fine. My question is...why does my way work? According to the Prototype documentation, the local variable myFunction should be thrown away after the eval. Any insight anyone can provide would be appreciated. Thanks in advance! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: evalScripts and functions
On May 24, 10:24 am, Michael mich...@michaelminella.com wrote: I understand how Prototype works with regards to the removal of script tags after evaling the results of an Ajax request. However, I was doing some research and am now starting to wonder why the way I declare functions works. According to the Prototype documentation, you need to declare the function and assign it to a global variable: myFunction = function() {alert('hi');} It's usually a good idea to avoid undeclared assignments (for clarity/ compatibility/robustness). Instead, consider assigning to a `window` property directly - window.myFunction = function(){ ... }; - or if you're extra cautious about assigning to an unpredictable and not-necessarily existent host object (which `window` happens to be), assign to an actual Global object - var global = (function(){return this;})(); global.myFunction = function(){ ... }; That makes sense. However, in all of my scenarios, I've declared functions like this: var myFunction = function myFunction() {alert('hi');} Named function expressions, which you're using here, have their own quirks across browsers. Be careful with them. and the calls to myFunction work just fine. My question is...why does my way work? According to the Prototype documentation, the local variable myFunction should be thrown away after the eval. Any insight anyone can provide would be appreciated. Thanks in advance! `myFunction` probably leaks into a global scope from within somewhere else. There's no other way for it to be declared globally, as long as `eval` (used internally by `evalScripts`) is spec-compliant and so evaluates in the scope of its caller - an internal anonymous function in case of Prototype.js. -- kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---