"Armel Asselin" <ar***@mobipocket.com> writes:
I'm working on a Javascript interpreter; when I execute this code I cannot
figure out why the inner "c" function returns the input widget as "this"...
could someone tell ??
Are you sure that is what it does?
<input type=button onclick="
function myobject(a) { function c() { return this; } var prop1=a;
this.youpla = c(); }
var i = new myobject();
alert(i.youpla.innerHTML);
" value="bonjour" />
the docs tells when calling "c( )" whithout "object." before it should pass
null as "this" but I get the widget ?
let's trace through the code.
--- start ---
function myobject(a) { function c() { return this; }
var prop1=a;
this.youpla = c(); }
--- Declaration of local function myobject ---
var i = new myobject();
--- call to myobject with this = new object ---
function c() { return this; }
--- declaration of local function c
var prop1=a;
--- local variable set to something, soon to be forgotten
this.youpla = c(); }
--- call to local function c ---
return this;
--- returns the global object, since function not called as method ---
--- youpla property of new object set to global object ---
--- local variable i set to new object
alert(i.youpla.innerHTML);
this should alert the innerHTML of the global/window object. No such
thing exists, so it should alert "undefined". It does for me in IE 6
and Opera 7.
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'