Skip to content
/ class Public

Small, fast, elegant, powerful, and cross platform JavaScript OOP library. Support class, inheritance, namespace, private and more.

License

Notifications You must be signed in to change notification settings

snandy/class

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to use

// if you run node.js, or CommonJS-compliant system
var Class = require('Class')

// if you use Class in a browser with AMD (like RequireJS):
require(['Class'], function(Class) {
    // the same code that uses Class
})

// or when you define your own module:
define(['Class'], function(Class) {
    // your Class-using code goes here
})

API

Define a single class 'Person'

Class('Person', function() {
    this.init = function(name) {
        this.name = name
    }
    this.getName = function() {
        return this.name
    }
    this.setName = function(name) {
        this.name = name
    }
    this.println = function() {
        alert('Name is ' + this.name)
    }
})
var p = new Person('John Backus')
console.log('Define a single class: ' + Person)
console.log('Create a instance of Person, his name is ' + p.getName())

Define a class 'Man' which inherit Person

Class('Man', Person, function(supr) {
    this.init = function(name, age) {
        // call super class constructor
        supr.init.call(this, name)
        this.age = age
    }
    this.getAge = function() {
        return this.age
    }
    this.setAge = function(age) {
        this.age = age
    }
    this.println = function() {
        alert('Name is ' + this.name + ', age is ' + this.age)
    }
})
var m1 = new Man('John McCarthy', 100)
m1.println()

Define a namespace class 'App.model.Man'

Class('App.model.Person', function() {
    this.init = function(name) {
        this.name = name
    }
    this.getName = function() {
        return this.name
    }
    this.setName = function(name) {
        this.name = name
    }
    this.println = function() {
        alert('Name is ' + this.name)
    }
})
var p = new App.model.Person('John Backus')
console.log('Create a instance of "App.model.Person", his name is ' + p.getName())

Define a class with private

Class('Person', function() {
    var privateVar = 'private vars'
    function privateFunc() {
        console.log(privateVar)
    }
    this.println = function() {
        privateFunc()
    }
})
var p = new Person()
p.println()

Specify the global naming

// The global naming
var Sohu = {}
Class.globalNamespace = Sohu

// define 'Sohu.Person'
Class('Person', function() {
    var privateVar = 'private vars'
    function privateFunc() {
        console.log(privateVar)
    }
    this.println = function() {
        privateFunc()
    }
})
var p = new Sohu.Person()
p.println()

Add statics for a class

Class.statics(Person, {
    prop1: 'xx',
    prop2: 'yy',
    method1: function() {xx},
    method2: function() {yy}
})
Person.prop1 // -》xx
Person.method1() // call static method

Add methods for a class's prototype

Class.methods(Person, {
    method1: function() {xx},
    method2: function() {yy}
})
var p1 = new Person('John', 29)
p1.method1()
p1.method2() 

Mixin Classes

var obj1 = {
    shout: function() {
        alert('I am a' + this.name)
    }
}
var obj2 = {
    setName: function(name) {
        this.name = name
    },
    setAge: function(age) {
        this.age = age
    }
}
Class('Person', function() {
    this.init = function(name, age) {
        this.name = name
        this.age = age
    }
})
Class.agument(Person, obj1, obj2)

Custom event

Class('Person', function() {
    this.init = function(name) {
        this.name = name
    }
    this.getName = function() {
        return this.name
    }
    this.setName = function(name) {
        this.name = name
    }
    this.println = function() {
        alert('Name is ' + this.name)
    }
})
var p = new Person('John Backus')
p.on('change', function(ev) {
    console.log(ev)
})

var num = 1;
document.onclick = function() {
    p.fire('change', 'clicked: ' + num++)
}  

AMD Usage

define('Person', ['Class'], function (Class) {
    return Class('Person', function() {
        this.init = function(name, age) {
            this.name = name
            this.age = age
        }
        this.getName = function() {}
        this.setName = function(name) {}
    })
})

About

Small, fast, elegant, powerful, and cross platform JavaScript OOP library. Support class, inheritance, namespace, private and more.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published