So I have cleaned up EvilScript, now a full featured downloadable thing. Its actually basically one function, about 30 lines. It allows you to write JS in a “classical” style, if thats your thing. The name “EvilScript” should give you an idea how I feel about the idea, but I spent this evening cleaning this up, so enjoy. Download the minified file here.
Using this is really simple. No support for public/private methods, but this is really just object cloning with inheritance:
var vehicle = {
_init:function(){
console.log('vi');
},
numwheels:0,
running:false,
start:function(){
this.running = true;
},
stop:function(){
this.running = false;
},
isRunning:function(){
return this.running;
},
setWheelNum:function(num){
this.numwheels = num;
},
getWheelNum:function(){
return this.numwheels;
}
};
var car = {
extend:vehicle,
_init:function(fueltype){
this.fueltype = fueltype;
},
fueltype:'',
getFuelType:function(){
return this.fueltype;
}
};
var myVehicle = evil.alloc(vehicle);
var myCar = evil.alloc(car, ['unleaded']);
log(myVehicle.isRunning());
myVehicle.start();
log(myVehicle.isRunning());
log('Car:');
log('fuel: '+ myCar.getFuelType());
log(myCar.isRunning());
myVehicle.start();
log(myCar.isRunning());
var evil = function(){
/**
* clone method copies objects
* @param object o
* @method c
* @return {void}
*/
function c(o){
var F = function(){};
F.prototype = o;
return new F();
}
return {
/**
* intantiate a new class,
* @method alloc
* @param Object o
* @param Array args
* @return Object o
*/
alloc:function(o, args){
if(o.extend !== undefined){
var ex = this.alloc(o.extend);
var tmp = c(o);
for(x in tmp){
ex[x] = tmp[x];
}
o = c(ex);
}else{
o = c(obj);
}
if(args){
o._init.apply(o, args);
}
return o;
}
};
}();