Code Monkey home page Code Monkey logo

class.js's Introduction

Class.js

A class & prototyping lib for JavaScript. Performant and resource light.

Prototyping doesn't have to be hard in js, or at least this tiny lib tries to make it that way.

What you may be used to (this example is in C#, but generally it is very similar in most languages):

class MyClass
{
    public MyClass()
    {

    }
}

or

class MyClass extends OtherClass
{
    public MyClass()
    {

    }
}

In c#, and in most programmatic languages, there is a certain order of creating classes. Breaking down the process in order:

  • I want to make a class
  • I want to name it "MyClass"
  • optionally I could extend MyClass with another class, in the above scenario it is "OtherClass"
  • I want a certain method to be called when it is instantiated

Javascript really doesn't have this ability by itself, but by using Class.js, we can preserve this exact order of programming. Also, we can take advantage of javascript's prototyping. Using Class.js to create a basic object:

//"MyClass" will be generated on the global context here, which is the "window" variable
Class('MyClass', {
    //"construct" is the method called on instantiation/construction
    construct: function() {
        alert('I am called when constructed');
    },
    //"otherMethod" is a generic method
    otherMethod: function() {
        alert('I am called when you want me to');
    }
});

//"MyClass" was generated, not lets instantiate a new instance of it
var myClass = new MyClass();

Extending (notice here that the anon function is going to be used as the construct function):

Class('OtherClass', function() {});

Class('MyClass', { extends: OtherClass,

    construct: function() {
        //this.parent === OtherClass
    }

});

In javascript with Class.js, we now gain the this ability and the order of creating classes. Breaking down the process in order:

  • I want to make a class
  • I want to name it "MyClass"
  • optionally I could extend MyClass with another class, in the above scenario it is "OtherClass"
  • I want a certain method to be called when it is instantiated

Anonymous extending:

Class('OtherClass', function() {});
var MyClass = OtherClass.extend({
    construct: function() {
        alert('I am called when constructed');
    }
});

Parent - Child relationships (hierarchy not limited):

Class('OtherClass', function() {});
var MyClass = OtherClass.extend({
    construct: function() {
        alert(this.parent.typeOf);
    }
});

Static properties and functions:

Class('MyClass', {
	Static: {
		items: [true, true, true],
		getItems: function() {
			return MyClass.items;
		}
	},
	construct: function() {},
	method: function() {}
});

var items = MyClass.getItems();

Namespaces:

Namespace('Project.Tools').
    Class('Hammer', {
        construct: function() {}
    }).
    Class('Nail', {
        construct: function() {}
    });

var hammer = new Project.Tools.Hammer(),
    nail = new Project.Tools.Nail();

"Class" & "Namespace" are fully compatible with apply and call for creating classes in whatever scope you like.

class.js's People

Contributors

kaisoellch avatar robertleeplummerjr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.