A simpler class framework

A

aum

Hi,

For the benefit of folks coming from other languages such as Python, I've
created a small framework that simplifies the creation and use of classes,
subclasses and instances.

http://www.freenet.org.nz/js/classframework.html

Features:
- freely mix classical and prototypal inheritance
- can wrap traditional javascript 'constructor function' classes
- more familiar feel to those coming in from other languages
- facilitates somewhat more readable code (IMHO)

So instead of having to use weird idioms like:

function F(arg) {
this.arg = arg;
}
F.prototype.toString = function() {
return "[F:arg="+this.arg+"]"
}

function G() {
F.apply(this, arguments)
}
G.prototype = new F()
G.prototype.toString = function() {
return "[G:arg="+this.arg+"]"
}

f = new F("foo")
g = new G("bar")

you can now do:

F = Class.subclass({
init: function(arg) {
this.arg = arg;
},
toString: function() {
return "[F:arg="+this.arg+"]"
}
})

G = F.subclass({
toString: function() {
return "[G:arg="+this.arg+"]"
}
})

f = F.instance("foo")
g = G.instance("bar")

Cheers
aum
 
P

petermichaux

Hi Aum,
Hi,

For the benefit of folks coming from other languages such as Python, I've
created a small framework that simplifies the creation and use of classes,
subclasses and instances.

http://www.freenet.org.nz/js/classframework.html

Features:
- freely mix classical and prototypal inheritance
- can wrap traditional javascript 'constructor function' classes
- more familiar feel to those coming in from other languages
- facilitates somewhat more readable code (IMHO)

So instead of having to use weird idioms like:

function F(arg) {
this.arg = arg;
}
F.prototype.toString = function() {
return "[F:arg="+this.arg+"]"
}

function G() {
F.apply(this, arguments)
}
G.prototype = new F()
G.prototype.toString = function() {
return "[G:arg="+this.arg+"]"
}

f = new F("foo")
g = new G("bar")

you can now do:

F = Class.subclass({
init: function(arg) {
this.arg = arg;
},
toString: function() {
return "[F:arg="+this.arg+"]"
}
})

G = F.subclass({
toString: function() {
return "[G:arg="+this.arg+"]"
}
})

f = F.instance("foo")
g = G.instance("bar")

I've seen similar syntax in other class constructor code. I don't
understand the need for the init function and this just doesn't look
like JavaScript.

The best tutorial I've found for class based inheritance is

http://kevlindev.com/tutorials/javascript/inheritance/index.htm

and some of the things I like about it since this system this system
seems so natural for JavaScript

http://peter.michaux.ca/article/1
http://peter.michaux.ca/article/49
http://peter.michaux.ca/article/50

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top