[Proto-Scripty] Re: local variables for for() array iteration
On Jan 20, 4:50 pm, Ran Berenfeld berenfeld...@gmail.com wrote: Thank you for the advise. one more / last question to clarify. In one of the articles about javascript scopes I read that using var inside a class constructor actually creates a private member of the class. is it true ? No. There are no classes in ECMAScript. Functions can be called using the new keyword to use them as constructors. Various patterns can be used in ECMAScript to emulate features of classic object inheritance. The var keyword is used to keep the scope of a variable to the execution context that it is declared within (ECMAScript only has function level scope). It has a role in emulating private members but does not do so by itself. The pattern most commonly used is Richard Cornford's module pattern: http://groups.google.com/group/comp.lang.javascript/msg/ 9f58bd11bd67d937 if this is true, It isn't. then should I avoid using for (var i=0;iarr.length;i++) like statements inside a class constructor ? A consequence being that every variable inside your constructor will become a global variable when the statement assigning to them is executed (which will be after the constructor is called). because then i would become a class member, and then using var i inside class function is not safe. am I right ? No. -- Rob -- 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: local variables for for() array iteration
Hi, Yes, you should use a `var` statement -- specifically, two of them. With your functions as quoted, you're falling prey to the Horror of Implicit Globals[1], meaning that both your `A` and `B` functions are using the *same* variable (a global variable), and so of course `B` is interfering with `A`'s loop. Corrected: function B() { var i; // == The new bit for(i=0;iarray_b_length;i++) { ... } } function A() { var i; // == The new bit for(i=0;iarray_a.length;i++) { B() ... } } Now `A` and `B` each have their own, independent, local `i` variable. Also worth reading up on the `var` keyword[2], which is sometimes misunderstood. [1] http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html [2] http://blog.niftysnippets.org/2008/03/poor-misunderstood-var.html HTH, -- T.J. Crowder Independent Software Engineer tj / crowder software / com www / crowder software / com On Jan 19, 1:31 pm, Ran Berenfeld berenfeld...@gmail.com wrote: Hello all sorry for this stupid question, but all these talks about the *this pointer and the variable scope in js got me confused. support I have 2 functions, one calling the other inside array iteration. can they both use the same local variable for array index ? should I use a var statement ? for example : function B() { for(i=0;iarray_b_length;i++) { ... } } function A() { for(i=0;iarray_a.length;i++) { B() ... } } Thanks Ran -- 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: local variables for for() array iteration
@Rüdiger: And last but not least, if this is a prototype mailing list you should of course use prototype, so your loops would look like this: Whether I'm using Prototype or not, I don't need to create a function and incur the overhead of a function call on every iteration (not to mention the complications around `this`) just to loop through an array. People are free to do that if they like, but it's not remotely necessary and it's certainly not always appropriate. There's nothing wrong with a nice, simple `for` loop. Just my two cents. -- T.J. Crowder Independent Software Engineer tj / crowder software / com www / crowder software / com On Jan 19, 3:18 pm, Rüdiger Kaatz rudirak...@gmail.com wrote: And last but not least, if this is a prototype mailing list you should of course use prototype, so your loops would look like this: $A(array_b_length).each(function(arrayElement) { // do something with the element... }); -- 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.
Re: [Proto-Scripty] Re: local variables for for() array iteration
Hmm, it' is the prototype way of doing it. I suppose it has to do with writing beautiful code, but that might be a matter of taste. Some like it and some don't :-) On Wed, Jan 19, 2011 at 11:59 PM, T.J. Crowder t...@crowdersoftware.com wrote: @Rüdiger: And last but not least, if this is a prototype mailing list you should of course use prototype, so your loops would look like this: $A(array_b_length).each(function(arrayElement) { // do something with the element... }); Whether I'm using Prototype or not, I don't need to create a function and incur the overhead of a function call on every iteration (not to mention the complications around `this`) just to loop through an array. People are free to do that if they like, but it's not remotely necessary and it's certainly not always appropriate. There's nothing wrong with a nice, simple `for` loop. Just my two cents. -- T.J. Crowder Independent Software Engineer tj / crowder software / com www / crowder software / com On Jan 19, 3:18 pm, Rüdiger Kaatz rudirak...@gmail.com wrote: And last but not least, if this is a prototype mailing list you should of course use prototype, so your loops would look like this: $A(array_b_length).each(function(arrayElement) { // do something with the element... }); -- 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. -- 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: local variables for for() array iteration
On Jan 19, 11:31 pm, Ran Berenfeld berenfeld...@gmail.com wrote: Hello all sorry for this stupid question, but all these talks about the *this pointer and the variable scope in js got me confused. A function's this keyword has nothing to do with scope. support I have 2 functions, one calling the other inside array iteration. can they both use the same local variable for array index ? Yes, if that's what you want to do. There are a number of ways to do that. should I use a var statement ? Yes, always. for example : function B() By convention, function names starting with a captial letter are reserved for constructors. { for(i=0;iarray_b_length;i++) You should declare i as a local variable (and I think you have a typo with array_b_lengh), so: for (var i=0; i array_b.length; i++) Variables that aren't declared locally become global variables at the point in execution when the statement that creates them is executed. If neither A or B are called, or if the for loops are never executed, this code will not create a global i variable. { ... } } function A() { for(i=0;iarray_a.length;i++) { B() ... } } If this code is run unmodified, when A is called a global variable i will be created. When A calls B, it will access the same variable. Since the for loop in B initially assigns 0 to i, its loop will run OK, but when control returns to A, the value of i will be whatever it was when B finished with it (in this case, array_b.length). So if array_a.length = array_b.length+1 then the for loop in A will only run one loop. -- Rob -- 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.
Re: [Proto-Scripty] Re: local variables for for() array iteration
Thank you for the advise. one more / last question to clarify. In one of the articles about javascript scopes I read that using var inside a class constructor actually creates a private member of the class. is it true ? if this is true, then should I avoid using for (var i=0;iarr.length;i++) like statements inside a class constructor ? because then i would become a class member, and then using var i inside class function is not safe. am I right ? Ran On Thu, Jan 20, 2011 at 12:59 AM, T.J. Crowder t...@crowdersoftware.comwrote: @Rüdiger: And last but not least, if this is a prototype mailing list you should of course use prototype, so your loops would look like this: Whether I'm using Prototype or not, I don't need to create a function and incur the overhead of a function call on every iteration (not to mention the complications around `this`) just to loop through an array. People are free to do that if they like, but it's not remotely necessary and it's certainly not always appropriate. There's nothing wrong with a nice, simple `for` loop. Just my two cents. -- T.J. Crowder Independent Software Engineer tj / crowder software / com www / crowder software / com On Jan 19, 3:18 pm, Rüdiger Kaatz rudirak...@gmail.com wrote: And last but not least, if this is a prototype mailing list you should of course use prototype, so your loops would look like this: $A(array_b_length).each(function(arrayElement) { // do something with the element... }); -- 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.comprototype-scriptaculous%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en. -- 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.