473,804 Members | 3,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Classes

I am getting an "Object Expected" error on line 0. Do you see anything
wrong with the following code:

var iTest = 60

function test(msg)
{
var NewMessage = msg;
StartIt(NewMess age);
function DisplayMessage( theMessage)
{
alert(iTest + theMessage);
}

function StartIt(theMess age)
{
var timer;
timer = setInterval("Di splayMessage('" + theMessage + "')",2000);
}

}
Thank you in advance

The email address is valid.

Mar 1 '06 #1
11 1446
un**********@mc hsi.com wrote:
I am getting an "Object Expected" error on line 0. Do you see anything
wrong with the following code:
Because DisplayMessage( ) is defined as a local variable of test().
setInterval() attempts to run it from a global context so it can't find it.

var iTest = 60

function test(msg)
{
var NewMessage = msg;
Varialbes starting with a capital letter usually signifies that they
will be used as a constructor - just a convention.

StartIt(NewMess age);
function DisplayMessage( theMessage)
{
alert(iTest + theMessage);
}

function StartIt(theMess age)
{
var timer;
timer = setInterval("Di splayMessage('" + theMessage + "')",2000);
var timer = setInterval(fun ction(){
DisplayMessage( theMessage);
},2000);
However that creates a closure and may cause memory leak problems in
some (buggy) browsers.

}

}

--
Rob
Mar 1 '06 #2
un**********@mc hsi.com wrote:
I am getting an "Object Expected" error on line 0. Do you see anything
wrong with the following code:
Plenty. However, let us begin with the fact that there are no classes in
the language version you use.
var iTest = 60
Avoid global variables.
function test(msg)
{
var NewMessage = msg;
This variable declaration is redundant, `msg' is already a local variable.
StartIt(NewMess age);
function DisplayMessage( theMessage)
{
alert(iTest + theMessage);
}

function StartIt(theMess age)
{
var timer;
timer = setInterval("Di splayMessage('" + theMessage + "')",2000);
}

}
DisplayMessage( ) and StartIt() are inner functions of test(). They are
not a method of `test' objects, nor is there a `test' class (see above).
setInterval() is in fact the same method as window.setInter val() which
evaluates its argument in global context always. However, because
DisplayMessage( ) is not defined in global context, only in the local
one if test(), you get the useless error message from Internet Explorer.
(Try the JavaScript/Error Console a Gecko-based or Opera-based browser
instead. See also <URL:http://jibbering.com/faq/#FAQ4_43>.)

A reasonable solution would include proper application of prototype-based
inheritance, something like

function Test(msg)
{
this.test = 60;
this.startIt(ms g);
}

Test.prototype = {
displayMessage: function test_displayMes sage(theMessage )
{
alert(this.test + theMessage);
},

startIt: function test_startIt(th eMessage)
{
this.timer = window.setInter val(
function()
{
this.displayMes sage(theMessage );
},
2000);
}
};
Test.prototype. constructor = Test;

var t = new Test("foo");

Caveat: This will not work in IE before version 5.0, and NN before
version 1.3. <URL:http://pointedears.de/scripts/js-version-info>

You will find that yours is quite a FAQ here. Please do research
before you post.
The email address is valid.


True, however a name would be nice. I, for one, do not like talking
to e-mail addresses (or companies); I like talking to people :)
PointedEars
Mar 1 '06 #3
Thomas 'PointedEars' Lahn wrote:
un**********@mc hsi.com wrote:
I am getting an "Object Expected" error on line 0. Do you see anything
wrong with the following code:


Plenty. However, let us begin with the fact that there are no classes in
the language version you use.
var iTest = 60


Avoid global variables.
function test(msg)
{
var NewMessage = msg;


This variable declaration is redundant, `msg' is already a local variable.
StartIt(NewMess age);
function DisplayMessage( theMessage)
{
alert(iTest + theMessage);
}

function StartIt(theMess age)
{
var timer;
timer = setInterval("Di splayMessage('" + theMessage + "')",2000);
}

}


DisplayMessage( ) and StartIt() are inner functions of test(). They are
not a method of `test' objects, nor is there a `test' class (see above).
setInterval() is in fact the same method as window.setInter val() which
evaluates its argument in global context always. However, because
DisplayMessage( ) is not defined in global context, only in the local
one if test(), you get the useless error message from Internet Explorer.
(Try the JavaScript/Error Console a Gecko-based or Opera-based browser
instead. See also <URL:http://jibbering.com/faq/#FAQ4_43>.)

A reasonable solution would include proper application of prototype-based
inheritance, something like

function Test(msg)
{
this.test = 60;
this.startIt(ms g);
}

Test.prototype = {
displayMessage: function test_displayMes sage(theMessage )
{
alert(this.test + theMessage);
},

startIt: function test_startIt(th eMessage)
{
this.timer = window.setInter val(
function()
{
this.displayMes sage(theMessage );
},
2000);
}
};
Test.prototype. constructor = Test;

var t = new Test("foo");

Caveat: This will not work in IE before version 5.0, and NN before
version 1.3. <URL:http://pointedears.de/scripts/js-version-info>

You will find that yours is quite a FAQ here. Please do research
before you post.
The email address is valid.


True, however a name would be nice. I, for one, do not like talking
to e-mail addresses (or companies); I like talking to people :)
PointedEars


Thank you both for your replies. I have learned a lot. I greatly
appreciate it.

Thanks again,
Kent

Mar 1 '06 #4
After implementing the code you provided I got the following error:
Line 19 Object doesn't support this property or method.
Line 19 is:
this.displayMes sage(theMessage );
If I change this to:
test_displayMes sage(theMessage );
it almost works. The error goes away but I get undefinedfoo. I would
assume this means the code doesn't "see" test = 60. Why would this be?
Also, is the change I made, the correct change to the code?

Thanks again for you help,
Kent

Mar 1 '06 #5
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:

A reasonable solution would include proper application of prototype-based
inheritance, something like

function Test(msg)
{
this.test = 60;
this.startIt(ms g);
}

Test.prototype = {
displayMessage: function test_displayMes sage(theMessage )
{
alert(this.test + theMessage);
},

startIt: function test_startIt(th eMessage)
{
this.timer = window.setInter val(
function()
{
this.displayMes sage(theMessage );
The "this" operator in this context will refer to the global object as
well, when the function is called by the setInterval timer.

A way to capture the correct value in the closure would be:
...
var self = this;
this.timer = window.setInter val(
function() {
self.displayMes sage(theMessage );
...

},
2000);
}
};
Test.prototype. constructor = Test;


This could be moved into the object literal too, or instead of
assigning to the prototype property, one could assing properties to
the existing prototype object, which already has this value.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 1 '06 #6
VK

Lasse Reichstein Nielsen wrote:
A way to capture the correct value in the closure would be: ...
var self = this;


"self" is a property of window object (self-reference to the current
window):
self.alert('OK' ); // equals window.alert('O K') in the global scope

Usually it is not recommended to override host object properies unless
required by the augmentation logic.

To fix the "incontinen ce of this" issue I would suggest to use some
unifirmed identifier instead - like $ (just a suggestion).

Mar 1 '06 #7
On 01/03/2006 07:45, VK wrote:
Lasse Reichstein Nielsen wrote:
A way to capture the correct value in the closure would be:
...
var self = this;
"self" is a property of window object (self-reference to the current
window):


What exactly does that have to do with anything? The variable defined
above will be a local variable; a property of the Variable object for
the test_startIt function. Resolution of this identifier through the
scope chain will find it in that location before the global object is
reached and searched.
self.alert('OK' ); // equals window.alert('O K') in the global scope
No-one denies that in most object models, both the self and window
global variables refer to the global object. It's just that most will
realise that it's irrelevant in this scenario.
Usually it is not recommended to override host object properies
unless required by the augmentation logic.
The global variable will not be overwritten.
To fix the "incontinen ce of this" issue I would suggest [...]


I would suggest that you learn to understand how ECMAScript-based
languages work before trying to correct others.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 1 '06 #8
VK

Michael Winter wrote:
I would suggest that you learn to understand how ECMAScript-based
languages work before trying to correct others.


I'm sorry, but you may drop this paternizing tone with me. Richard
alone is more then enough - "the horse cannot hold both" :-)

Please: I did *not* say that the posted code will not work - it will. I
just questionned the real need to shadow the global "self" reference in
the function.

Mar 1 '06 #9
On 01/03/2006 12:40, VK wrote:

[snip]
Please: I did *not* say that the posted code will not work - it will.
But you did imply that it will "override host object properies [sic]",
when such a statement is quite simply false.
I just questionned the real need to shadow the global "self"
reference in the function.


You haven't made a case for why it should be avoided. There is no direct
reference to the global object at all, let alone through a global
'self'. The word 'self' as an identifier is quite appropriate in this
case, and it isn't so frequently used nor important enough that its
redefinition would be confusing (as might be the case with 'document' or
'window', for example).

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
9
1655
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these applications need to serialise the classes to/from the same files but only the GUI app needs the full range of class methods. Now, the rendering app needs to be ported to multiple OS's but the GUI doesn't. In order to reduce the time/cost of porting I'd...
9
6642
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to use the common example, I have a situation where I am implementing many types of Shapes. My current way of thinking is, well, since they are all the same type, let's just put them all in the same file. The include file would be "shapes.h" and it...
12
21239
by: Langy | last post by:
Hello I'm fairly new to C++ but have programmed several other languages and found most of c++ fairly easy (so far!). I've come to a tutorial on classes, could someone please tell me why you would need to use a class? Perhaps you could also give an example on when it might be used rather than an alternative method.
4
1812
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but understanding others peoples logic isnt (that doesnt mean they are wrong). i prefer designing classes for the currect job and making tangible 'things' classes , not overdoing it with loads of classes or inheritance.. it seems easier to make...
2
9508
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how to do that? Thanks. Tsung-Yu
18
2049
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise. Yet, in my understanding, attributes are only __gc and __value class specific and do not apply to __nogc classes. Is this correct ? If so, how is the packing alignment of __nogc classes stored ?
6
2944
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
0
2035
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid this topic getting lost before people interested in the problem notice it. The important tricks to the solution are two: 1) make the custom classes take a TEMPLATE argument which defines their BASE class 2) EMBED the custom classes in a "Traits"...
2
1919
by: Amu | last post by:
i have a dll ( template class) ready which is written in VC++6. But presently i need to inherit its classes into my new C#.net project.so if there is some better solution with u then please give me the solution.
0
10580
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
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 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...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
agi2029
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...
0
5525
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...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.