473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

"this" object

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
4 11137


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
"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
> 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mike Kamermans | last post by:
I'm trying to determine where inside an XHTML's DOM a javascript function is triggered, but when I use the construction of calling a function with "this" as parameter and then checking the event...
9
by: aden | last post by:
I have read the years-old threads on this topic, but I wanted to confirm what they suggest. . . Can the this pointer EVER point to a type different from the class that contains the member...
3
by: Oliver Saunders | last post by:
can someone please tell me why everytime i try and pass this to a js function i can't seem to use it for anything: <html> <head> <script language="javascript"> function a(fromlink) {...
6
by: Marty | last post by:
Hi, I have a class that I modified to be static. It is now a public sealed class and all function are static, no more constructor but a init() function to do the constructor job. This class...
2
by: danny.dion | last post by:
Hi ! I have a question about JScript : I have an object class wich dynamically creates a control in the page. Then it binds an event to that control, pointing on one of its methods (the...
2
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
14
by: white lightning | last post by:
How to have <select onchange="this.form.submit()"and also a Submit button on one form? I have something like this: <form action="<?php $_SERVER; ?>" method="post"...
2
by: bednarz.thomas | last post by:
I have the following business Object(s): public class ParentObject { public ParentObject(string somestring) { ...- } ....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.