栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

js继承 Base类的源码解析

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

js继承 Base类的源码解析

// timestamp: Tue, 01 May 2007 19:13:00

// 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){
//参数大于一个时
if (arguments.length > 1) { // extending with a name/value pair
//获得proto的祖先
var ancestor = this[source];
var value = arguments[1];
//如果value(第二个参数)是function,并且祖先对象存在,在重载函数中调用base时
if (typeof value == "function" && ancestor && /bbaseb/.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 用一个对象列表来扩展
var extend = base.prototype.extend;

//如果是扩展属于原型的方法或属性,先遍历其重载Object的3个方法
if (base._prototyping) {
var key, i = 0, members = ["constructor", "toString", "valueOf"];
while (key = members[i++]) {
//如果是重载了这些方法
if (source[key] != Object.prototype[key]) {

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

var extend = base.prototype.extend;

base._prototyping = true;

var proto=new this;

extend.call(proto, _instance);

delete base._prototyping;

// create the wrapper for the constructor function

var constructor = proto.constructor;

var klass = proto.constructor = function(){

if (!base._prototyping) {

if (this._constructing || this.constructor == klass) { // instantiation
this._constructing = true;
constructor.apply(this, arguments);
delete this._constructing;
}

else { // casting
var object = arguments[0];
if (object != null) {
(object.extend || extend).call(object, proto);
}
return object;
}
}
};
// build the class interface

for (var i in base){
klass[i] = this[i];
}

klass.ancestor = this;
klass.base = base.base;
klass.prototype = proto;
klass.toString = this.toString;

extend.call(klass, _static);
// class initialisation 如果存在init函数 调用
if (typeof klass.init == "function")
klass.init();
return klass;
};
// initialise
base = base.extend({
constructor: function(){
this.extend(arguments[0]);
}
}, {
ancestor: Object,
base: base,
implement: function(_interface){
if (typeof _interface == "function") {
// if it's a function, call it
_interface(this.prototype);
}
else {
// add the interface using the extend() method
this.prototype.extend(_interface);
}
return this;
}
});
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/118683.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号