Re: Attach a class method to event handler



On May 30, 4:43 am, Thomas 'PointedEars' Lahn <PointedE...@xxxxxx>
wrote:
Peter Michaux wrote:
On May 29, 5:35 pm, Thomas 'PointedEars' Lahn <PointedE...@xxxxxx>
wrote:
Peter Michaux wrote:
[...]
MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
document.getElementById('my_div').onclick = function ( e )
{
MyClass.prototype.doSomethingWith( e || event );
}
}

While this is not the least equivalent to the original, `MyClass' is the
bound "variable" of the closure then.

or perhaps even

MyClass.prototype.doSomething = function( ) {};
MyClass.prototype.assignFun = function( )
{
document.getElementById('my_div').onclick =
MyClass.prototype.doSomethingWith;
}
It is getting worse. Now the method of the prototype object will be called
as a method of the Global Object.

It wasn't clear what the code in the original post was doing or even
if the doSomething function uses "this". Without a complete question a
complete answer is not possible.

You are winding around the issue.

No I'm not.

It does not matter whether the purpose of the code was clear (to you) or
not;

If the question is not clear then many answers are possible.

using the `this' reference in a prototype method

There was no use of "this" in the original question.

and using the
reference to the prototype object there are clearly not the same thing, nor
are assigning the reference to a prototype method versus assigning a
reference to a Function object that calls this prototype method (as
inherited through the prototype chain). So it cannot be expected to have
the same outcome, and is therefore not the least equivalent to one another.

But it may have acceptable outcome. The question was not clear. That
was actually one point I was making with my original answer.

[snip]


Peter
.



Relevant Pages

  • Re: How do I call a base class member function that has been overriden?
    ... The effect of these two lines (except from storing the classA function as ... object which has only classB's empty prototype object as prototype. ... You still have the problem of not having A's doSmth around at all. ...
    (microsoft.public.scripting.jscript)
  • Re: simply super
    ... instead of from the prototype object of that constructor), ... next revision of the ECMAScript Specification if the proposed feature is ... Norris.prototype.kick and the prototype chain would work its way. ... functionality added in the Norris.prototype.kick method. ...
    (comp.lang.javascript)
  • Re: prototype object and arrays - Im confused
    ... prototype object works.. ... var A = function ... Can someone explain me why JS doesn't create array copy for every new ... return TRUE for 'r' while doing it with prototype will return FALSE. ...
    (comp.lang.javascript)
  • Re: with and prototype
    ... it is worth being aware that it has consequence that will probably not be obvious to many. ... When a function object is created a prototype object is created for that function. ... This prototype object is given a property called "constructor" to which a reference to the function is assigned. ... So if the function is used as a constructor for new object instances they will all inherit a - constructor - property through their prototype, that refers back to the function that constructed them. ...
    (comp.lang.javascript)
  • Re: object methods act like properties
    ... I want to have a static property but it won't be in the object ... prototype method. ... Thus once the prototype method has been called once, ... original instance that triggered the prototype chain? ...
    (comp.lang.javascript)