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 -. > > <snip> > >> <script type="text/javascript"> > >> 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); > >> } > >> </script>