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.