Skip to content

crowebird/js_classes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js_classes (v1.1.1)

A js function that adds simple class extensibility to javascript without prototyping or requiring any external libraries.

I wanted a truer class based experience with javascript, but most of the extensibility scripts out there for JS did not fit what I was looking for, so I created my own.

This allows you to extend classes, gives you a constructor that is auto called upon instantiaion, and gives you access to super for extended classes (as well as create abstract classes and methods).

Note There are only PUBLIC and PRIVATE methods, and child classes only have access to parent PUBLIC methods. There is no PROTECTED.

How It Works

js_classes.extend('SomeClass', function() {
  var self;
  var somePrivateVar;
  
  return self = {
    _construct: function(_arg) {
      somePrivateVar = _arg;
    },
    
    getVar: function() {
      return somePrivateVar;
    },
    
    getVarFormatted: function() {
      return "\n" + self.getVar() + "\n";
    }
  };
})

var mySomeClass = new SomeClass(10);
console.log(mySomeClass.getVar()); //10

You call js_classes.extend to create a new class. js_classes.extend has two ways to be called:

js_classes.extend(name, function);

//name - the name of the class
//function - the callable function that is the class being created.

Function works as a class using javascript scope. The function returns a json object, these will be the public methods/variables. Anything meant to be private will be left outside of the json object (see the example above).

The variable self allows all the public methods to be accessed from other public methods, and gives the private scope access to the public scope.

Extending

When extending, simply call extend on a class that was created with js_class.extend.

Any public method that is overwritten will have access to a _super function which will call the parent function.

js_classes.extend('Animal', function() {
  var self;
  var _sound,
      _type;
      
  return self = {
    _construct: function(sound, type) {
      _sound = sound || '...';
      _type = type || 'unknown'
    },
    
    makeSound: function() {
      return _sound;
    },
    
    getType: function() {
      return _type;
    }
  };
});

Animal.extend('Cat', function() {
  var self;
  
  return self = {
    _construct: function() {
      self._super._construct('meow', 'cat');
    }
  }
});

var myCat = new Cat();
console.log(myCat.makeSound()); //meow
console.log(myCat.getType()); //cat

_Note that _construct is not a true public method. Creating an instance will auto call the construct method for you, and then remove it as a callable function.

Limitations

Remember that there are only PUBLIC and PRIVATE methods. If we took the above example and changed Cat to:

Animal.extend('Cat', function() {
  var self;
  
  return self = {
    _construct: function() {
      self._super._construct('meow', 'cat');
    },
    
    makeSound: function() {
      return self._super.makeSound();
    },
    
    getType: function() {
      return _type;
    }
  }
});
var myCat = new Cat();
console.log(myCat.makeSound()); //meow
console.log(myCat.getType()); //_type is not defined

makeSound will still fire properly since we directly reference the parent function call. getType however will fail because in the scope of Cat, _type is undefined.

Instance Of

Traditional javascript instanceof will not work with my implementation of classes, since an instantiated Class really becomes a reference to a json Object, not the function Class itself.

To fix this, all classes have a method _instanceOf that will allow you to check if an instance is an instanceof some class, or is a subclass of some class.

Using the same Animal, Cat example:

var myAnimal = new Animal();
var myCat = new Cat();

console.log(myAnimal._instanceOf(Animal)); //true
console.log(myAnimal._instanceOf(js_classes)); //true
console.log(myAnimal._instanceOf(Cat)); //false
console.log(myCat._instanceOf(Cat)); //true
console.log(myCat._instanceOf(Animal)); //true

_Note that instanceOf cannot be overwritten

Abstract

Abstract classes can also be created, these are defined by naming the class function as abstract when creating the class:

js_classes.extend('AbstractTestClass', function abstract() {
  var self;
  return self = {
    myAbstractFunction: function abstract() {},
    myDefinedFunction: function() {
      return 1;
    }
  };
});

Abstract classes cannot be instantiated. Trying to create an instance of them will throw an error.

Notice in the example above we created two public methods:

  • myAbstractFunction which is declared abstract by naming the function abstract. The function must be empty, adding a body to it will throw an error. Any class that extends this class must implement this function.
  • myDefinedFunction which is a normal public method, any child methods that do not override it will (in this case) return 1

Now if we extended AbstractTestClass, using what we know, we will only have to implement myAbstractFunction, otherwise an error will be thrown when we instantiate our new class:

AbstractTestClass.extend('ExtendedTestClass', function() {
  var self;
  return self = {
    myAbstractFunction: function () {
      return 'I have implemented myAbstractFunction'
    }
  };
});
var myInstance = new ExtendedTestClass();
myInstance.myAbstractFunction(); //I have implemented myAbstractFunction
myInstance.myDefinedFunction(); //1

Namespaces

js_classes can also be given a namespace'd name to match PSR-0 compliance for example:

//Filepath: /root/some/folder/MyClass.js
js_classes.extend('root.some.folder.MyClass', function abstract() {
  return {
    someFunction: function abstract(){}
  };
});
//Filepath: /root/some/folder/other/MyClass.js
root.some.folder.MyClass.extend('root.some.folder.other.MyClass', function() {
  return {
    someFunction: function() {
      return 'Implemented!';
    }
  };
});
var instance = root.some.folder.other.MyClass();
instance.someFunction(); //Implemented!

About

A js function that adds simple class extensibity to javascript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published