JavaScript 面向對象框架:dejavu
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); } });
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!