By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

"this" object

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Armel Asselin wrote:
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>
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a
"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.'
Jul 20 '05 #3

P: n/a
> 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...

Jul 20 '05 #4

P: n/a
"Armel Asselin" <ar***@mobipocket.com> writes:
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
--
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.'
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.