473,405 Members | 2,261 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,405 software developers and data experts.

Class Problem

Hello NG

I have a problem with classes in javascript. When I define my class and
make an instance more than one time, it does not contian their own
variables:

var Counter = (function() {
var counter = 0;

function Constructor() {
this.fncInit();
}

Constructor.prototype.fncInit = function() {
counter++;
alert(counter);
}
})();

var counter1 = new Counter();
var counter2 = new Counter();
var counter3 = new Counter();

The counter increases by one each time, but it should always be 1.

Thanks for any help.

Jim
Jul 23 '05 #1
2 1271
On 20/06/2005 15:19, Jim Red wrote:
I have a problem with classes in javascript.
You certainly do: there are no classes. ECMAScript works within a
prototype object model, not a class-based one.
When I define my class and make an instance more than one time, it does not contian their own
variables:

var Counter = (function() {
var counter = 0;

function Constructor() {
this.fncInit();
}
The counter variable here is effectively a private static member.
Instead, use:

function Counter() {
var counter = 0;

this.fncInit = function() {
++counter;
alert(counter);
};

this.fncInit();
}

[snip]
})();


You don't actually return the Constructor function in the posted code.
This would mean there is no function with which to apply the new
operator. Did that arise because you didn't type it into the post, or
did the return statement not appear in the original, either?

var MyObject = (function() {
/* Private static variables and methods here */

function constructor() {
/* Private instance variables and methods,
* and public 'priviledged' methods here.
*/
}
/* Public static (assigned directly to
* constructor) and prototyped variables and
* methods here
*/

return constructor;
})();

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Michael Winter wrote:
On 20/06/2005 15:19, Jim Red wrote:
I have a problem with classes in javascript.

You certainly do: there are no classes. ECMAScript works within a
prototype object model, not a class-based one.
When I define my class and make an instance more than one time, it
does not contian their own variables:

var Counter = (function() {
var counter = 0;

function Constructor() {
this.fncInit();
}

The counter variable here is effectively a private static member.
Instead, use:

function Counter() {
var counter = 0;

this.fncInit = function() {
++counter;
alert(counter);
};

this.fncInit();
}

[snip]
})();

You don't actually return the Constructor function in the posted code.
This would mean there is no function with which to apply the new
operator. Did that arise because you didn't type it into the post, or
did the return statement not appear in the original, either?


oops, sorry. I forgot about it.
//Jim

var MyObject = (function() {
/* Private static variables and methods here */

function constructor() {
/* Private instance variables and methods,
* and public 'priviledged' methods here.
*/
}
/* Public static (assigned directly to
* constructor) and prototyped variables and
* methods here
*/

return constructor;
})();

[snip]

Mike

Jul 23 '05 #3

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

Similar topics

1
by: Steve | last post by:
Hello, I'm encountering an unexpected behavior when using the "new" modifier in a derived class to hide an inherited base class property. I use "new" intentionally so I can change the Type of...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
21
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
8
by: tshad | last post by:
I cannot seem to get the asp:textbox to use classes. Style works fine. I am trying to set the textbox to act like a label in some instance so it doesn't have a border, readonly and the background...
4
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
5
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
3
by: TamusJRoyce | last post by:
Hello. This is my first thread here. My problem has probably been came across by a lot of people, but tutorials and things I've seen don't address it (usually too basic). My problem is that I...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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,...
0
isladogs
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...

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.