dejavu.jsJavaScript 面向对象框架

联合创作 · 2023-09-22

dejavu 在JavaScript原型继承的基础上提供了经典的继承形式,使得其他语言开发者可以轻松转向JavaScript。

dejavu 主要特性:

  • 类(具体的、抽象的、final类)

  • 接口

  • 混入(这样你可以使用某种形式的多重继承)

  • 私有成员和受保护成员

  • 静态成员

  • 常量

  • 函数上下文绑定

  • 方法签名检查

  • 扩展和借用vanilla类

  • 自定义instanceOf,支持接口

  • 两个版本:普通版本和AMD优化版本

  • 每个版本都有两种模式:严格模式(执行很多检查)和宽松模式(无检查)

示例代码:

var Person = Class.declare({
    // although not mandatory, it's really useful to identify
    // the class name, which simplifies debugging
    $name: 'Person',

    // this is a protected property, which is identified by
    // the single underscore. two underscores denotes a
    // private property, and no underscore stands for public
    _name: null,
    __pinCode: null,

    // class constructor
    initialize: function (name, pinCode) {
        this._name = name;
        this.__pinCode = pinCode;

        // note that we're binding to the current instance in this case.
        // also note that if this function is to be used only as a
        // callback, you can use $bound(), which will be more efficient
        setTimeout(this._logName.$bind(this), 1000);
    },

    // public method (follows the same visibility logic, in this case
    // with no underscore)
    getName: function () {
        return this._name;
    }

    _logName: function () {
        console.log(this._name);
    }
});
浏览 5
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报