3.0 beta 2

I'm trying to use your library to understand OO in JavaScript

I know. I'm horrible. It wouldn't be so bad, since it all makes sense given JavaScript's prototyping approach. The problem is I don't understand what 'this' refers to in all the various places in this function?

Can I trouble you to enlighten me? If you have a wishlist I might buy something off it...

Active.System.Object.subclass = function(){

var constructor = function(){this.init()};
for (var i in this) {constructor[i] = this[i]}
constructor.prototype = new this();
constructor.superclass = this;
return constructor;

April 13,
This particular piece of code is really, really bad :-)
Still, let’s try.

As an introduction you may want to read this:


Line 1: Active.System.Object.subclass = function(){

var Active is an object, the root of the hierarchy.
Active.System is also an object (System is a property of Active)
Active.System.Object is a function (Object is a method of System). To be precise, it is not just a function, it is a constructor function, which is used to create objects of type Active.System.Object (like this: var obj = new Active.System.Object;).

We are creating a method 'subclass' of the Active.System.Object. Again, Active.System.Object is a constructor function, which represents a class, not an object instance. It is OK to create a method or a property of a function because functions themselves behave like objects.

Line 2: var constructor = function(){this.init()};

Our 'subclass' method should return a constructor function, which we create here. Because the constructor is created automatically, the actual object initialization code should be somewhere else, i.e. in a special 'init' method. So each object constructor just calls 'init' method of a newly created object. Here 'this' keyword refers to the newly created object, when subclass constructor runs.

Line 3: for (var i in this) {constructor[i] = this[i]}

This code copies all properties and methods from the base class constructor to the derived class constructor. Note, it is NOT object properties, it is constructor function properties. Keyword 'this' refers to the base class constructor, i.e. Active.System.Object function.

Line 4: constructor.prototype = new this();

The 'prototype' property of the constructor of the derived class should point to the base class object instance. Here we create a new instance of the base class by calling the base class constructor function with the keyword 'new'. Again, 'this' refers to the base class constructor, i.e. Active.System.Object function.

Line 5: constructor.superclass = this;

We also create special 'superclass' property, which provides quick access to the base class constructor from within the derived class. It is very useful when you want to overload a method in the derived class but still be able to call the base class implementation of the same method.

Line 6: return constructor;

End of story.

Alex (ActiveWidgets)
April 14,
Wow, I didn't expect this much detail! It's greatly appreciated! :)

April 15,

This topic is archived.

See also:

Back to support forum


Version 2

Looking for AW 2.6 ?
The old site is here.