[jQuery] Re: Passing scope to a nested anonymous function

2008-06-19 Thread Michael Geary

Guys, you're both right.

We don't actually know what meppum's exact requirement is. Does the code
really have to strictly follow that exact design pattern? Or would it be
possible to use a simpler, more idiomatic approach to achieve the same
goals?

Either way, it doesn't hurt to get the original code working, so Ariel's
patch is just what's needed there.

But Henry, I did have a similar reaction to the code as you: "Sure, you can
get this to work, but is it really necessary? There are much simpler ways to
do the same thing."

Meppum, can you clarify what the real requirements are, so we will all be a
little less confused? :-)

Thanks,

-Mike

> From: Ariel Flesler
> 
> He is probably showing a dummy example to represent a more 
> complex, real situation.
> So there's no sense in judging the exact code he posted.
> 
> That was the solution to his problem, that he should be able to adapt.
> 
> If you want to suggest a whole new approach to, what you 
> suppose, he needs.. then propose it directly to him, not by 
> judging my fix.

> > From: Henry
> >
> > While that answer would be superficially effective (in the sense of 
> > resulting in code that would behave as specified) it is a stupidly 
> > inefficient approach, and so not something that should be 
> > suggested as a correction to the original code.
> >
> > ...
> >
> > So the above would be better as:-
> >
> > function myFunction(){
> > }
> > myFunction.prototype.foo = "hi";
> > myFunction.prototype.foo2 = function(){
> > this.foo = "hi back";
> > };



[jQuery] Re: Passing scope to a nested anonymous function

2008-06-19 Thread Ariel Flesler

He is probably showing a dummy example to represent a more complex,
real situation.
So there's no sense in judging the exact code he posted.

That was the solution to his problem, that he should be able to adapt.

If you want to suggest a whole new approach to, what you suppose, he
needs.. then propose it directly to him, not by judging my fix.

Cheers

--
Ariel Flesler
http://flesler.blogspot.com


Henry ha escrito:
> On Jun 19, 4:04 am, Ariel Flesler wrote:
> > On 18 jun, 17:45, meppum wrote:
> > }).call(this.foo2); ---> }).call(this);
>
> While that answer would be superficially effective (in the sense of
> resulting in code that would behave as specified) it is a stupidly
> inefficient approach, and so not something that should be suggested as
> a correction to the original code.
>
> You have proposed changing the - foo2Function - to:-
>
> function foo2Function() {
> (function() {
> this.foo = "hi back";
> }).call(this);
> }
>
> - but if the - this - that is the argument to the call method resolves
> as a reference to the correct object then the whole thing can be
> replaced with the considerably simpler and more efferent:-
>
> function foo2Function() {
>  this.foo = "hi back";
> }
>
> More efficient, because it avoids the creation of a function object
> each time the - foo2Function - is called, along with the call to -
> call -.
>
> 
> >> 
> >> function myFunction() {
> >> this.foo = "hi";
> >> this.foo2 = foo2Function;
>
> If a function is going to be used as a constructor (with the - new -
> keyword) then assigning constant values to properties of the this
> object within the constructors is less efficient and more unnatural
> (in javascript terms) than assigning those values to the object
> referred to by the function objects - prototype - property.
>
> >> }
> >
> >> function foo2Function() {
> >> (function() {
> >> this.foo = "hi back";
> >> }).call(this.foo2);
> >> }
>
> So the above would be better as:-
>
> function myFunction(){
> }
> myFunction.prototype.foo = "hi";
> myFunction.prototype.foo2 = function(){
> this.foo = "hi back";
> };
>
> >> function test() {
> >> var obj = new myFunction();
> >
> >> alert(obj.foo);
> >
> >> obj.foo2();
> >
> >> alert(obj.foo);
> >> }
> >> 


[jQuery] Re: Passing scope to a nested anonymous function

2008-06-19 Thread Henry

On Jun 19, 4:04 am, Ariel Flesler wrote:
> On 18 jun, 17:45, meppum wrote:
> }).call(this.foo2); ---> }).call(this);

While that answer would be superficially effective (in the sense of
resulting in code that would behave as specified) it is a stupidly
inefficient approach, and so not something that should be suggested as
a correction to the original code.

You have proposed changing the - foo2Function - to:-

function foo2Function() {
(function() {
this.foo = "hi back";
}).call(this);
}

- but if the - this - that is the argument to the call method resolves
as a reference to the correct object then the whole thing can be
replaced with the considerably simpler and more efferent:-

function foo2Function() {
 this.foo = "hi back";
}

More efficient, because it avoids the creation of a function object
each time the - foo2Function - is called, along with the call to -
call -.


>> 
>> function myFunction() {
>> this.foo = "hi";
>> this.foo2 = foo2Function;

If a function is going to be used as a constructor (with the - new -
keyword) then assigning constant values to properties of the this
object within the constructors is less efficient and more unnatural
(in javascript terms) than assigning those values to the object
referred to by the function objects - prototype - property.

>> }
>
>> function foo2Function() {
>> (function() {
>> this.foo = "hi back";
>> }).call(this.foo2);
>> }

So the above would be better as:-

function myFunction(){
}
myFunction.prototype.foo = "hi";
myFunction.prototype.foo2 = function(){
this.foo = "hi back";
};

>> function test() {
>> var obj = new myFunction();
>
>> alert(obj.foo);
>
>> obj.foo2();
>
>> alert(obj.foo);
>> }
>> 


[jQuery] Re: Passing scope to a nested anonymous function

2008-06-18 Thread Ariel Flesler

}).call(this.foo2); ---> }).call(this);

--
Ariel Flesler
http://flesler.blogspot.com/

On 18 jun, 17:45, meppum <[EMAIL PROTECTED]> wrote:
> I have the below code and I'd like the alerts to say "hi" then "hi
> there" but it looks like I'm losing scope inside the anonymous
> function. This is a simplified version of a more complicated function
> I am trying to implement. I'm not sure if jquery can help with this,
> and I'm fine using vanilla JS code, but I've read the scoping
> tutorials and can't figure it out...
>
> 
>     
>         
>                 function myFunction() {
>                     this.foo = "hi";
>                     this.foo2 = foo2Function;
>                 }
>
>                 function foo2Function() {
>                     (function() {
>                         this.foo = "hi back";
>                     }).call(this.foo2);
>                 }
>
>                 function test() {
>                     var obj = new myFunction();
>
>                     alert(obj.foo);
>
>                     obj.foo2();
>
>                     alert(obj.foo);
>                 }
>         
>     
>     
>
>     
>