I have a HTML page with a form with 2 buttons like this
....
<input type="button" value="Add" onClick="ClickA dd();"/>
<input type="button" value="Reset" onClick="ClickR eset();"/>
....
I also have this javascript code:
----------------------------------------
function ClickAdd() {
setTimeout(test .Add, 100);
//test.Add();
};
function ClickReset() {
setTimeout(test .Add, 100);
//test.Reset();
};
Test = function () {
this.total = 0;
};
Test.prototype. Add = function() {
this.total++;
alert(this);
};
Test.prototype. Reset = function(i) {
this.total = 0;
alert(this);
};
Test.prototype. toString = function() {
return (this.total);
};
test = new Test();
-------------------------------------------
the thing is:
in the ClickAdd function, if i call test.Add() directly, is works ok
But if I call it using setTimeout, the /alert(this)/ shows
[object Window]
Why? And what should i do to make it work as i want? 4 1948
Hiya
setTimeout is a method of the window object, calling setTimout is
therefore a call to window.setTimeo ut.
The keyword 'this' is defined in docs as follows:
"The this keyword refers to the current object. In general, in a method
this refers to the calling object"
Because window.setTimeo ut uses "test.Add" only as a function ( rather
than a function which is a method of a Test object ) then the object
found by 'this' is the window object.
To get your script to work add an extra argument to the add and reset
prototypes then change the arguments in the setTimeout call:
Test.prototype. Add = function( thisObj ) {
thisObj.total++ ;
alert(thisObj);
};
Test.prototype. Reset = function(thisOb j) {
thisObj.total = 0;
alert(thisObj);
};
function ClickAdd() {
setTimeout(test .Add, 100, test );
};
function ClickReset() {
setTimeout(test .Reset, 100, test);
};
It aint pretty but it seems to work ok
tihu wrote:
[included minimum quotation] Telmo Costa wrote: Test = function () { this.total = 0; }
Telmo, you should declare your identifiers; variables with the `var'
keyword. Furthermore, there is no need for a FunctionExpress ion here,
a FunctionDeclara tion suffices for the constructor definition:
function Test()
{
this.total = 0;
}
[...]
test = new Test(); [...] Because window.setTimeo ut uses "test.Add" only as a function ( rather than a function which is a method of a Test object )
Only loosely speaking, and that it is _window_.setTim eout() does not matter
regarding this. All functions are methods; globally available functions
are methods of the Global Object (ECMAScript 3 Final, section 15). If only
the Function object `test.Add' refers to is [[Call]]ed, that object is
called as a method of the Global Object because the base object component
of the Reference value is the Global Object, no longer `test' (13.2.1).
Therefore, the `this' value, which refers to the caller (or the object that
is [[Construct]]ed, see section 10.1.7), refers to the Global Object in the
code of the called Function object.
then the object found by 'this' is the window object.
No, it is the Global Object augmented with host-defined properties.
Its `window' property is one of those property and refers to to the
Global Object itself in many, if not all, HTML document object models.
To get your script to work add an extra argument to the add and reset prototypes then change the arguments in the setTimeout call:
Test.prototype. Add = function( thisObj ) { thisObj.total++ ; alert(thisObj); };
Test.prototype. Reset = function(thisOb j) { thisObj.total = 0; alert(thisObj); };
function ClickAdd() { setTimeout(test .Add, 100, test ); };
function ClickReset() { setTimeout(test .Reset, 100, test); };
It aint pretty but it seems to work ok
Every implementation that supports a Function object reference as first
argument of window.setTimeo ut() also supports FunctionExpress ions.
window.setTimeo ut(function() { test.Reset() }, 100);
There is no need to modify the signature of either method of the Test
prototype object. That said, there is no need to use a function reference
here at all because `test' is globally available (but should be declared a
variable anyway):
window.setTimeo ut("test.Reset( )", 100);
(I recommend against using identifiers starting with capital letter for
functions, unless they are intended to be a constructor or factory.)
Please quote the minimum of what you are referring to:
<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>
<URL:http://safalra.com/special/googlegroupsrep ly/>
PointedEars
Thanks. I think I got it. Telmo, you should declare your identifiers; variables with the `var' keyword. Furthermore, there is no need for a FunctionExpress ion here, a FunctionDeclara tion suffices for the constructor definition:
.... (I recommend against using identifiers starting with capital letter for functions, unless they are intended to be a constructor or factory.)
So, for a base function/object i should go with FunctionDeclara tion:
function test() {
...
}
var t = new test();
(is it just a recommendation, or is there something more?)
But, for an inner function it should go with the FunctionExpress ion:
var test.innerTest = function() {
...
};
innerTest is now a static method of the static global method test.
furthermore, it can be instatiated this way:
var inner = new test.innerTest( );
right?
telmo
Telmo Costa wrote: Thanks. I think I got it.
You're welcome. Please also learn to quote properly. Telmo, you should declare your identifiers; variables with the `var' keyword. Furthermore, there is no need for a FunctionExpress ion here, a FunctionDeclara tion suffices for the constructor definition: ... (I recommend against using identifiers starting with capital letter for functions, unless they are intended to be a constructor or factory.)
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ So, for a base function/object i should go with FunctionDeclara tion:
function test() { ... }
No, test() /is/ your constructor:
var t = new test();
^^^^^^^^
Therefore:
function Test()
{
...
}
(is it just a recommendation, or is there something more?)
There is no syntax rule. But you will observe that all host objects
follow that convention. It avoids confusion, one way or the other.
But, for an inner function it should go with the FunctionExpress ion:
var test.innerTest = function() { ... };
No, I said you should declare _identifiers_. `test.innerText ' is none,
it is a property access. Therefore, the above is a syntax error.
test.innerTest = function() {
...
};
is just fine. What do you mean by "inner function" anyway?
innerTest is now a static method of the static global method test.
No, it is not. Furthermore there are no static methods in ECMAScript
implementations in HTML user agents.
furthermore, it can be instatiated this way:
var inner = new test.innerTest( );
right?
No, see above. And "instantiat ed", if this class-based term can even be
applied to this prototype-based language, is the object referred to with
`inner', where `test.innerTest ' is its constructor (and a reference to it
can be obtained with `inner.construc tor' then.)
HTH
PointedEars This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ken |
last post by:
Hello
I am trying to change the color of a font in a text box.
I have been reading about and trying various examples found it this
group, but still can't get it right.
Here is where I am now:
<script language="JavaScript">
|
by: yawnmoth |
last post by:
I'm trying to write a script to make images fade in and out using the
varrious CSS attitrubes that browsers use for that purpose. In an a
tag, surrounded an image, I pass to the fade function the variable
this, and when I try to edit the style on that variable, I get an
error "object.style has no properties". Why wouldn't it? And what
can I do to fix it? Here's the page / code:
http://www.frostjedi.com/terra/dev/test.html
|
by: yb |
last post by:
Hi,
Is there a standard for the global 'window' object in browsers? For
example, it supports methods such as setInterval and clearInterval, and
several others.
I know that w3c standardized several parts of the DOM, but this does
not include the window object.
Thank you
|
by: Andrew Poulos |
last post by:
With the following code I can't understand why this.num keeps
incrementing each time I create a new instance of Foo. For each instance
I'm expecting this.num to alert as 1 but keeps incrementing.
Foo = function(type) {
this.num = 0;
this.type = type
this.trigger();
}
Foo.prototype.trigger = function() {
|
by: rain_c1 |
last post by:
hi,
i think this is a little exercise for real experts, but i suffer from
real headaches because of it... :-\
i have an object method (method1), that calls setTimeout with an other
method (method2) as parameter which calls further another method
(method3), but i do not know how to realize it.
it is important to use object context and not static members, because
method3 uses a member (more precisely a constructor parameter) that
| |
by: Hansen |
last post by:
Hi!
I have a problem with an object not yet being initialized when I try to
access it. Hence I would like to wait for the object to be initialized. I
have the following code:
try
{
if(gridArray == null)
{
|
by: Cylix |
last post by:
This is my object:
###################################################
function msgList() {
this.t = 5000;
this.timer = timer;
}
// ---------------------------------------
// ::: Methods for msgList
// ---------------------------------------
|
by: -Lost |
last post by:
I am calling setTimeout within the context of an object, and whilst this exists, it
refuses to be passed along to the function I call. For example:
$elemById('id').change = function()
{
// the function returns the object itself, 'this' exists
alert(this); //
setTimeout('alert(this);',1000); //
}
|
by: Atli |
last post by:
Hi everybody.
This is not so much a problem, since I have already managed to find a solution, but my solution requires the use of the eval() function, which I just hate to use.
The problem is this.
I have an object that has a function. This function needs to call itself in a setTimeout call. Using the "this" keyword from within the callback function will obviously not work, seeing as it would reference the new function rather than the...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |