"this" object

A

Armel Asselin

Hello,

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 ??

<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 ?

please help
Armel
 
M

Martin Honnen

Armel said:
Hello,

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 ??

<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 ?

In my view the this object should be the global object which is the
window object, and it is that with Netscape and with Opera. Only IE6/Win
makes the this object the containing <form> object, no idea why. I don't
think it is the <input> widget itself. Why do you think so? Which
browser have you tried?


<html>
<head>
<title>this object</title>
</head>
<body>
<form name="formName">
<input type="button"
name="buttonName"
onclick="function myobject(a) {
function c() {
return this;
}
var prop1=a;
this.youpla = c();
}
var i = new myobject();
alert('i.youpla: ' + i.youpla + '\ni.youpla == i: ' +
(i.youpla == i) + '\ni.youpla == this: ' + (i.youpla == this) +
'\ni.youpla == window: ' + (i.youpla == window) + '\ni.youpla ==
document.formName: ' + (i.youpla == document.formName));"
value="bonjour">
</form>
</body>
</html>
 
L

Lasse Reichstein Nielsen

Armel Asselin said:
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
 
A

Armel Asselin

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.

I used IE 5...
 
L

Lasse Reichstein Nielsen

Armel Asselin said:
I used IE 5...

I was testing with just the posted code, so with no form element
around it. If I add the form, IE 6 also reports the innerHTML of
the form.

It is perhaps an artifact of how IE makes the properties of the
form visible to the HTML event handler. It is still weird, and
incorrect according to the ECMAScript standard.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,246
Latest member
softprodigy

Latest Threads

Top