31i73-class 中文文档教程

发布于 11年前 浏览 22 项目主页 更新于 3年前

31i73-class

一个简单的node.js类库

API

类(statics [, instanced])

  • statics - An object containing all static class properties.
    This may also contain the special property _init for declaring the initialiser, and _inherits for declaring a single or multiple classes to inherit from
  • instanced - An object containing all instance class properties

Create a class

var class = require('31i73-class');

var Person = class({
    _init: function(name){
        this.name = name;
        this.age = undefined;
    }
},{
    get_name: function(){ return this.name; },
    set_name: function(set){ this.name = set; },

    get_age: function(){ return this.age; },
    set_age: function(set){ this.age = set; }
});

var bob = new Person('bob');
bob.set_age(42);

Static methods

var Person = class({
    _init: function(name){
        this.name = name;
        this.age = undefined;
    },
    create_adult: function(name){
        var create = new Person(name);
        create.set_age(24);
        return create;
    },
    create_child: function(name){
        var create = new Person(name);
        create.set_age(12);
        return create;
    }
},{
    get_name: function(){ return this.name; },
    set_name: function(set){ this.name = set; },

    get_age: function(){ return this.age; },
    set_age: function(set){ this.age = set; }
});

var littly_timmy = Person.create_child('Timmy');

Inheritence

var Animal = class({
    _init: function(name){
        this.name = name;
        this.soundeffect = undefined;
    }
},{
    set_soundeffect: function(path){
        this.soundeffect = new Soundeffect(path);
    }
});

var Horse = class({
    _inherits: Animal,
    _init: function(){
        Animal.call(this,'horse');
        this.set_soundeffect('horse.wav');
    }
});

//Multiple inheritence
var Mule = class({
    _inherits: [
        Horse,
        Donkey
    ],
    _init: function(){
        Animal.call(this,'mule');
        this.set_soundeffect('mule.wav');
    }
});

//a missing initialiser causes the first inherited classes initialiser to be called, instead
var Shire_horse = class({
    _inherits: Horse
});

31i73-class

A simple node.js class library

API

class( statics [, instanced] )

  • statics - An object containing all static class properties.
    This may also contain the special property _init for declaring the initialiser, and _inherits for declaring a single or multiple classes to inherit from
  • instanced - An object containing all instance class properties

Create a class

var class = require('31i73-class');

var Person = class({
    _init: function(name){
        this.name = name;
        this.age = undefined;
    }
},{
    get_name: function(){ return this.name; },
    set_name: function(set){ this.name = set; },

    get_age: function(){ return this.age; },
    set_age: function(set){ this.age = set; }
});

var bob = new Person('bob');
bob.set_age(42);

Static methods

var Person = class({
    _init: function(name){
        this.name = name;
        this.age = undefined;
    },
    create_adult: function(name){
        var create = new Person(name);
        create.set_age(24);
        return create;
    },
    create_child: function(name){
        var create = new Person(name);
        create.set_age(12);
        return create;
    }
},{
    get_name: function(){ return this.name; },
    set_name: function(set){ this.name = set; },

    get_age: function(){ return this.age; },
    set_age: function(set){ this.age = set; }
});

var littly_timmy = Person.create_child('Timmy');

Inheritence

var Animal = class({
    _init: function(name){
        this.name = name;
        this.soundeffect = undefined;
    }
},{
    set_soundeffect: function(path){
        this.soundeffect = new Soundeffect(path);
    }
});

var Horse = class({
    _inherits: Animal,
    _init: function(){
        Animal.call(this,'horse');
        this.set_soundeffect('horse.wav');
    }
});

//Multiple inheritence
var Mule = class({
    _inherits: [
        Horse,
        Donkey
    ],
    _init: function(){
        Animal.call(this,'mule');
        this.set_soundeffect('mule.wav');
    }
});

//a missing initialiser causes the first inherited classes initialiser to be called, instead
var Shire_horse = class({
    _inherits: Horse
});
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文