dejavu.jsJavaScript 面向?qū)ο罂蚣?/h1>dejavu 在JavaScript原型繼承的基礎(chǔ)上提供了經(jīng)典的繼承形式,使得其他語言開發(fā)者可以輕松轉(zhuǎn)向JavaScript。
dejavu 主要特性:
-
類(具體的、抽象的、final類)
-
接口
-
混入(這樣你可以使用某種形式的多重繼承)
-
私有成員和受保護(hù)成員
-
靜態(tài)成員
-
常量
-
函數(shù)上下文綁定
-
方法簽名檢查
-
擴(kuò)展和借用vanilla類
-
自定義instanceOf,支持接口
-
兩個(gè)版本:普通版本和AMD優(yōu)化版本
-
每個(gè)版本都有兩種模式:嚴(yán)格模式(執(zhí)行很多檢查)和寬松模式(無檢查)
示例代碼:
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);
}
});
瀏覽
25
dejavu 在JavaScript原型繼承的基礎(chǔ)上提供了經(jīng)典的繼承形式,使得其他語言開發(fā)者可以輕松轉(zhuǎn)向JavaScript。
dejavu 主要特性:
-
類(具體的、抽象的、final類)
-
接口
-
混入(這樣你可以使用某種形式的多重繼承)
-
私有成員和受保護(hù)成員
-
靜態(tài)成員
-
常量
-
函數(shù)上下文綁定
-
方法簽名檢查
-
擴(kuò)展和借用vanilla類
-
自定義instanceOf,支持接口
-
兩個(gè)版本:普通版本和AMD優(yōu)化版本
-
每個(gè)版本都有兩種模式:嚴(yán)格模式(執(zhí)行很多檢查)和寬松模式(無檢查)
示例代碼:
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);
}
});
評論
圖片
表情
