国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

js繼承 Base類的源碼解析

2024-05-06 14:16:17
字體:
供稿:網(wǎng)友
// timestamp: Tue, 01 May 2007 19:13:00
/*
base2.js - copyright 2007, Dean Edwards
http://www.opensource.org/licenses/mit-license
*/
// You know, writing a javascript library is awfully time consuming.
//////////////////// BEGIN: CLOSURE ////////////////////
// =========================================================================
// base2/Base.js
// =========================================================================
// version 1.1
var Base = function(){
// call this method from any other method to invoke that method's ancestor
};
Base.prototype = {
extend: function(source){
//參數(shù)大于一個時
if (arguments.length > 1) { // extending with a name/value pair
//獲得proto的祖先
var ancestor = this[source];
var value = arguments[1];
//如果value(第二個參數(shù))是function,并且祖先對象存在,在重載函數(shù)中調(diào)用base時
if (typeof value == "function" && ancestor && //bbase/b/.test(value)) {
// get the underlying method
var method = value;
// override
value = function(){
var previous = this.base;
this.base = ancestor;
//上溯到父類對象
var returnValue = method.apply(this, arguments);
this.base = previous;
return returnValue;
};
value.method = method;
value.ancestor = ancestor;
}
this[source] = value;
}
else
if (source) { // extending with an object literal 用一個對象列表來擴(kuò)展
var extend = Base.prototype.extend;
/**
* 1.擴(kuò)展原型方法和屬性 2.
*/
//如果是擴(kuò)展屬于原型的方法或?qū)傩?先遍歷其重載Object的3個方法
if (Base._prototyping) {
var key, i = 0, members = ["constructor", "toString", "valueOf"];
while (key = members[i++]) {
//如果是重載了這些方法
if (source[key] != Object.prototype[key]) {
/**
* 逐個擴(kuò)展,用call的原因是要將extend的上下文改為要擴(kuò)展的源this,
* 既是新建對象的父類對象
*/
extend.call(this, key, source[key]);
}
}
}
else
if (typeof this != "function") {
// if the object has a customised extend() method then use it
extend = this.extend || extend;
}
// copy each of the source object's properties to this object
for (key in source)
if (!Object.prototype[key]) {
extend.call(this, key, source[key]);
}
}
return this;
},
base: Base
};
Base.extend = function(_instance, _static){ // subclass
/**
* Base類原型的擴(kuò)展別名,將這個當(dāng)成一個方法調(diào)用
*/
var extend = Base.prototype.extend;
/**
* build the prototype,創(chuàng)建原型
* 設(shè)置原型標(biāo)志
*/
Base._prototyping = true;
/**
* 創(chuàng)建一個Base的實例,初始化繼承部分
* 繼承方式大致還是以下方式
* function A(){}
* function B(){
* this.b=[];
* }
* A.prototype=new B();//A繼承B的所有屬性和方法
* 這種繼承方式會有一個問題,B中聲明的對象(如b)以prototype的形式
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 山阳县| 喜德县| 辽中县| 阜阳市| 广宗县| 海口市| 鹰潭市| 杭锦旗| 鹤壁市| 吉林市| 德兴市| 武陟县| 运城市| 东丰县| 武威市| 榆中县| 高雄市| 肇东市| 常宁市| 东宁县| 娄烦县| 中卫市| 绥芬河市| 鞍山市| 林州市| 三台县| 怀仁县| 和林格尔县| 济阳县| 汾西县| 黔西县| 分宜县| 吴忠市| 高台县| 资阳市| 萨嘎县| 衡阳县| 陈巴尔虎旗| 高尔夫| 重庆市| 高雄市|