logo       

Re: The shared variable problem: msg#00019

web.kupu.devel

Subject: Re: The shared variable problem

Hi!


Duncan Booth wrote:
The problem:

Any variable assigned to 'this' in the base class constructor becomes a class variable in the subclasses: but is NOT shared between different subclasses (if I further subclasses MyClass1 then subclasses of MyClass1 would also share the same instance as MyClass1 itself).

This probably isn't what we want, since if we want a class variable we could create it by assigning to MyBase.prototype.bar.

The solution, it seems to me, is that we need to ensure that the base class constructor is called whenever an instance of a derived class is constructed. Javascript won't do this automatically, but it is easy enough to do it manually. (For anyone reading this who doesn't know Ecmascript backwards: apply is a method of all functions which allows you to call the function with an explicit 'this' value. If the function takes parameters these are simply specified after the 'this' value.)

Recently I found this: http://www.webreference.com/js/column80/9.html

It seems to describe the same problem and to propose a very similar solution.


Cheers,

Yuppie


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise