Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #11972

Re: onclick=function(){obj.meth()}

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: onclick=function(){obj.meth()}
Date 2012-02-21 00:57 +0100
Organization albasani.net
Message-ID <jhuml0$5f7$1@news.albasani.net> (permalink)
References <jp45k75qbcjbb5v8tavnmognds11hjihdh@4ax.com> <jhu992$b78$1@news.albasani.net> <1fb5k75j7mbjn04fm251o1omb0u1l1rbqv@4ax.com> <jhugq7$qkn$1@news.albasani.net> <g6i5k7dkde7q52cilanl1qqo2jtcdqjsl9@4ax.com>

Show all headers | View raw


On 2012-02-20 23:48, Puzzled wrote:
> This is what I'm trying to do:
> 
> onclick = "this.some_method_identifier( arg, arg )"
> 
> where 'this' is the instance's self-reference.
> 
> It shouldn't be hard to do

You think so? I'd say it is impossible in your case. Inline event
handlers are not executed in the scope of your x object. When they get
activated, they set |this| to the element node they're attached to. You
would need some other way to get the proper context for the row() method
from the _outside_. The only way to do that is to make your x object
globally available.

This is (IMHO) the biggest drawback of inline event handlers. Another is
that you're mixing code and markup. That's why I suggested using DOM
methods.

(What I would actually do in your case is different again: I would set
_one_ handler for the table, and use it to delegate events to the right
methods. But before you attempt that, you should be a little more
comfortable with the DOM and its methods.)

> -- the terp certainly knows that
> 'this' is a special case, and should be able to grasp that when I
> use it in a binding, I want its current value bound.   That's the
> whole point of oo after all -- to give a "free" reference to an
> instance such that the same code can be used for all instances.

If you want to pass a reference, then you should do that. What you're
actually doing is placing an identifier (or keyword) inside a string for
later evaluation in a different scope. Unless that identifier is global,
this won't work.

(BTW, I'm assuming that "terp" is the interpreter?)

> But apparently the terp doesn't dereference 'this' at that moment
> or do any trickery to create the expected binding.  Instead it
> evaluates 'this' at the time the esr is called, which doesn't
> work.

The |this| value is indeed special. Its value always depends on how a
function is called, and it will not be inherited by nested functions
(hence the need for a proxy if you want to preserve it).

> Yet people do make it work, I suppose, in some way.  I thought
> the proxy and the anon function were the required magic, be
> evidently not so, so now I'm back where I started, neck deep in
> confusion and ignorance.

I've shown you one way to make it work, and it doesn't require any
magic. The example I've posted is self-contained, it will run as-is and
do what you describe. We're willing to help you with the confusion and
ignorance, but you need to be open for advice. The way you want to write
it cannot work. Read the example again and try to understand what's
going on. The DOM methods may seem overly verbose at first, but there
are lots of wrappers and libraries out there to make your life easier.
(Or more confusing, in the case of some libraries ;-)


- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

onclick=function(){obj.meth()} Puzzled <scratching.head@example.com> - 2012-02-20 14:02 -0500
  Re: onclick=function(){obj.meth()} "Tom de Neef" <tdeneef@qolor.nl> - 2012-02-20 20:20 +0100
    Re: onclick=function(){obj.meth()} Jake Jarvis <pig_in_shoes@yahoo.com> - 2012-02-20 20:39 +0100
      Re: onclick=function(){obj.meth()} "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-21 00:34 +0000
        Re: onclick=function(){obj.meth()} Stefan Weiss <krewecherl@gmail.com> - 2012-02-21 02:13 +0100
          Re: onclick=function(){obj.meth()} Richard Cornford <Richard@litotes.demon.co.uk> - 2012-02-21 04:05 -0800
        Re: onclick=function(){obj.meth()} Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-21 12:47 +0100
          Re: onclick=function(){obj.meth()} John G Harris <john@nospam.demon.co.uk> - 2012-02-21 15:59 +0000
  Re: onclick=function(){obj.meth()} Stefan Weiss <krewecherl@gmail.com> - 2012-02-20 21:09 +0100
    Re: onclick=function(){obj.meth()} "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-20 20:29 +0000
      Re: onclick=function(){obj.meth()} Puzzled <scratching.head@example.com> - 2012-02-20 16:50 -0500
        Re: onclick=function(){obj.meth()} "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-21 11:33 +0000
    Re: onclick=function(){obj.meth()} Puzzled <scratching.head@example.com> - 2012-02-20 15:47 -0500
      Re: onclick=function(){obj.meth()} Jake Jarvis <pig_in_shoes@yahoo.com> - 2012-02-20 22:38 +0100
        Re: onclick=function(){obj.meth()} Puzzled <scratching.head@example.com> - 2012-02-20 17:12 -0500
      Re: onclick=function(){obj.meth()} Gene Wirchenko <genew@ocis.net> - 2012-02-20 14:04 -0800
        "The this value" (Was: onclick=function(){obj.meth()}) Stefan Weiss <krewecherl@gmail.com> - 2012-02-21 01:37 +0100
          Re: "The this value" (Was: onclick=function(){obj.meth()}) Gene Wirchenko <genew@ocis.net> - 2012-02-20 17:20 -0800
          Re: "The this value" (Was: onclick=function(){obj.meth()}) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-21 13:44 +0100
            Re: "The this value" (Was: onclick=function(){obj.meth()}) Stefan Weiss <krewecherl@gmail.com> - 2012-02-21 22:31 +0100
              Re: "The this value" (Was: onclick=function(){obj.meth()}) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-22 10:55 +0100
                Re: "The this value" (Was: onclick=function(){obj.meth()}) John G Harris <john@nospam.demon.co.uk> - 2012-02-22 15:58 +0000
        Re: onclick=function(){obj.meth()} "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-21 01:30 +0000
      Re: onclick=function(){obj.meth()} Scott Sauyet <scott.sauyet@gmail.com> - 2012-02-20 13:43 -0800
      Re: onclick=function(){obj.meth()} Stefan Weiss <krewecherl@gmail.com> - 2012-02-20 23:17 +0100
        Re: onclick=function(){obj.meth()} Puzzled <scratching.head@example.com> - 2012-02-20 17:48 -0500
          Re: onclick=function(){obj.meth()} Stefan Weiss <krewecherl@gmail.com> - 2012-02-21 00:57 +0100
          Re: onclick=function(){obj.meth()} "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-21 01:03 +0000
            Re: onclick=function(){obj.meth()} Gene Wirchenko <genew@ocis.net> - 2012-02-20 19:05 -0800
              Re: onclick=function(){obj.meth()} Richard Cornford <Richard@litotes.demon.co.uk> - 2012-02-21 04:14 -0800
                Re: onclick=function(){obj.meth()} Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-21 13:54 +0100
                Re: onclick=function(){obj.meth()} Gene Wirchenko <genew@ocis.net> - 2012-02-21 10:17 -0800
                Re: onclick=function(){obj.meth()} Scott Sauyet <scott.sauyet@gmail.com> - 2012-02-21 11:57 -0800
                Re: onclick=function(){obj.meth()} Gene Wirchenko <genew@ocis.net> - 2012-02-21 13:39 -0800
                Re: onclick=function(){obj.meth()} Scott Sauyet <scott.sauyet@gmail.com> - 2012-02-22 05:07 -0800
            Re: onclick=function(){obj.meth()} Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-21 12:58 +0100
        Re: onclick=function(){obj.meth()} "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-21 00:45 +0000
          Re: onclick=function(){obj.meth()} Stefan Weiss <krewecherl@gmail.com> - 2012-02-21 02:22 +0100
        Re: onclick=function(){obj.meth()} Puzzled <scratching.head@example.com> - 2012-02-21 08:12 -0500
          Re: onclick=function(){obj.meth()} Scott Sauyet <scott.sauyet@gmail.com> - 2012-02-21 11:14 -0800

csiph-web