Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Jake Jarvis Newsgroups: comp.lang.javascript Subject: Re: onclick=function(){obj.meth()} Date: Mon, 20 Feb 2012 22:38:22 +0100 Lines: 69 Message-ID: <9qfsqeF75tU1@mid.uni-berlin.de> References: <1fb5k75j7mbjn04fm251o1omb0u1l1rbqv@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Z8AWO6lMVGZ4mORQnOhzKw3KlUZq+8yTIKNnJyaG8scNk9Evk= User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: <1fb5k75j7mbjn04fm251o1omb0u1l1rbqv@4ax.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:11963 On 20.02.2012 21:47, Puzzled wrote: > On Mon, 20 Feb 2012 21:09:05 +0100, > Stefan Weiss wrote: > >> On 2012-02-20 20:02, Puzzled wrote: >>> Now I'm trying to convert the code to javascript's version of oo >>> style >> ... >>> function row( i ) >>> { >>> var rec_s = '>> 'id="foo'+i+'" '+ >>> 'onclick=function(){alert('+i+')}>'+i+''+ >>> ''; >>> return rec_s ; >>> } >> >> Why are you wrapping "function(){ ... }" around your event handler code? >> That's not what "javascript's version of oo style" is. Anonymous >> functions have their place, but they need to be part of an expression, >> or it's syntax error. Drop the "function(){ ... }" bit (and put the >> remaining onclick value in quotes, while you're at it). >> >> There are some other weak spots in your code, but this should resolve >> the syntax error. > > What I supplied was the simplest form that gives the error. What > I'm actually trying to do is use a method as the event service > routine, so a somewhat more accurate version of the code would > be: > > // row is called as x.rec_row, > // where x is the obj instance identifier > > // esr is the event service routine declared in x > > // i is the integer id of the record that can be selected > // for further processing by clicking the link > > function row( i ) > { > var proxy = this ; // create a closure-alias for 'this' > var rec_s = ' 'id="foo'+i+'" '+ > 'onclick=function(){proxy.esr(i)}>'+i+''+ > ''; > return rec_s ; > } > > It's my understanding that to use a method as an esr, I've to > create and use a proxy for 'this' in the event binding as here, > and use an anon function so as to be able to pass arguments. Is > that not so? When you use HTML "intrinsic event attributes" you write as attribute content the statements you want executed 2 (note the delimiting with " (details in the HTML specs)) When you script the HTML document by manipulating the higher level DOM representation you assign an anonymous function anchorRef.onclick = function () { p.esr(2); }; -- Jake Jarvis