From: John Lam Date: 2006-05-18T23:31:28+09:00 Subject: Re: Is there an equivalent to instance_eval in JavaScript? ------=_Part_121217_30709381.1147962686594 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Thanks, Marcus! I'll let you know if some corner cases crop up (my JavaScript is pretty rusty). BTW, what is it with JavaScript and guys from Sweeden? :) Cheers, -John http://www.iunknown.com On 5/18/06, Marcus Andersson wrote: > > No, I don't think so. But you can probably add it. > > Object.prototype.instance_eval =3D function(code_or_function) { > var func =3D null; > if(typeof(code_or_function) =3D=3D "function") { > func =3D code_or_function; > } else if(typeof(code_or_function) =3D=3D "string") { > func =3D function() { eval(code_or_function); } > } > if(func) { > return func.apply(this); > } > } > > var o =3D { helloPhrase : "hello" }; > o.instance_eval(function() { > alert(this.helloPhrase); > }); > > o.instance_eval("alert(this.helloPhrase);"); > > Although you might not want to add it to Objects prototype and do it as > a function that takes the receiving object as a parameter. I'm also not > 100% sure that this captures the full semantics of instance_eval altough > I guess it's quite close. > > /Marcus > > > ------=_Part_121217_30709381.1147962686594--