自己研究的javascript封装,欢迎大家来拍

wfyfjplfjj 2010-10-23 05:27:52

var $ = new (function _$(){
this.extend = function (dest,src,force){
for(var name in src){
if(!force && dest[name]){
alert(name);
continue;
}
dest[name] = src[name];
}
};
})();

$.extend(Object.prototype,{
extends:function(clazz){
//将父类保存起来
this._super = clazz;
return this;
},
super:function(){
//包含对父类的引用,同时将父类的方法和属性
var args = Array.prototype.slice.apply(arguments);
this.super = this._super.apply(this,args);
$.extend(this,this.super,true);
}
});
function A(name,value){
this.name = name;
this.value = value;
}

A.prototype.test = function(){
alert('a.test');
}
function B(){
this.extends(A).super({a:'a'},'bb');
alert(this.name.a);
}
var b = new B();
...全文
262 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wfyfjplfjj 2010-10-25
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 cavst 的回复:]

前来学习学习....
[/Quote]
zao
wfyfjplfjj 2010-10-24
  • 打赏
  • 举报
回复
不再开贴了,继续奉上,没有采用上面说的大写保留字的做法,因为怕误写,而是使用$public代替,目前已经实现继承父类,且仅继承公共方法,可以调用父类的构造器,下一步,实现接口机制。不多说了,上代码

//=============================Utilities=========================
var $util = new (function(){
//指定class是否已加载
this.classExist = function(clazz){
return !(!clazz.prototype._type);
};
this.findClass=function(clazz){
return this.classList[clazz];
};
//初始化
this.initClass=function(type){
if(!this.initing.status){
this.initing.status=true;
this.initing.type=type;
}
};
this.endInitClass=function(){
this.initing.status=false;
};
this.initing={status:false,type:"Class"};
//扩展
this.extend = function (dest,src,force,scope,filter){
for(var name in src){
if(name != "_type" && name != "inited" && (force || (!force && dest[name])) && (!filter || (filter&&filter(src[name]))) ){
if(scope){
src[name].scope = scope;
}
dest[name] = src[name];
}
}
};
//继承
this.inherit = function(current,parent){
this.extend(current,parent.prototype,true,null,function(obj){
return obj.scope && $util.isFn(obj)?obj.scope === "$public":true;
});

current._super.fn=parent.prototype._constructor.fn;
current.constructor.prototype = current;
};
//遍历一个对象中的属性,调试用
this.iterateTest = function(obj){
for(var name in obj){
alert(name);
}
};
//判断是否为函数
this.isFn = function(obj){
return typeof(obj) === "function";
};
})();
//=============================Utilities END=========================
//=============================BaseClass============================
function Class(){}
//为基类型添加属性和方法
$util.extend(Class.prototype,{
_type:"Class",//类名
getClass:function(){//获取类名
return this._type;
},
inited:false,//已经初始化标志
$public:function(obj){//公共作用域
if(this.inited){
return;
}
$util.extend(this.constructor.prototype,obj,true,'$public');
$util.extend(this,obj,true);
},
$private:function(obj){//私有作用域
if(this.inited){
return;
}
$util.extend(this.constructor.prototype,obj,true,'$private');
$util.extend(this,obj,true);
},
_constructor:{args:null,fn:function(){}},
$constructe:function(fn){//构造器
if(this.inited){
return;
}
this._constructor.fn=this.constructor.prototype._constructor.fn = fn;
},
$super:function(){//调用父类的方法
this._super.fn.apply(this,Array.prototype.slice.apply(arguments));
},
_super:{fn:function(){},args:null},
classend:function(){//类定义结束符
//如果是执行类初始化过程,不执行构造器
this.inited=true;
if($util.initing.type===this._type){
this._constructor.fn.apply(this,this._constructor.args);
}
}
},true);
//基类模板加载
new Class();
//=============================BaseClass============================
//=============================Prototype=========================
//为函数添加作用域
$util.extend(Function.prototype,{
scope:"$public"
});

//为对象添加继承方法
$util.extend(Object.prototype,{
$extends:function(parent){
//如果类没有初始化过
if(!this.inited){
if(!parent){
parent = Class;
}
var reg = /function\s([a-zA-z]+)\(/;//获取类名
this._type=this.constructor.prototype._type=this.constructor.toString().match(reg)[1];//初始化类型
$util.initClass(this._type);
if(!$util.classExist(parent)){
new parent();
}
$util.inherit(this,parent);
}
//保存构造器参数
this._constructor.args=Array.prototype.slice.apply(this.constructor.arguments);
}
},true);
//=============================Prototype END========================
//=============================Test========================
function A(){
this.$extends(Class);//继承自基类
this.$constructe(function(a){
this.name=a;
});

//公共属性和方法
this.$public({
test:function(){
alert('test');
}
});
//私有属性和方法
this.$private({
test1:function(){
alert('test1');
}
});
this.classend();//类定义结束
}
function B(){
this.$extends(A);//继承自A类,且仅有A类的public 方法
this.$constructe(function(a,b){
this.$super(a);
//this.test1();//not run
});

this.classend();//类定义结束
}
var b = new B("test");
b.test();
//=============================Test END========================
zjhiphop2006 2010-10-24
  • 打赏
  • 举报
回复
public和extends在js中指关键字,楼主需要修改一下封装,如下:
var $util = new (function(){
//指定class是否已加载
this.classExist = function(clazz){
return !(!clazz.prototype.type);
}
//扩展
this.extend = function (dest,src,force,scope,filter){
for(var name in src){
if(!force && dest[name]){
continue;
}
if(scope){
src[name].scope = scope;
}
if(filter){
if(!filter(src[name])){
continue;
}
}
dest[name] = src[name];
}
};
//继承
this.inherit = function(current,parent){
this.extend(current,parent.prototype,true,null,function(obj){
return obj.scope === "public";
});
current.constructor.prototype = current;
};
//遍历一个对象中的属性,调试用
this.iterateTest = function(obj){
for(var name in obj){
alert(name);
}
};
//判断是否为函数
this.isFn = function(obj){
return typeof(obj) === "function";
};
})();
//基类型
function Class(){}
//为基类型添加属性和方法
$util.extend(Class.prototype,{
_type:"Class",//类名
getClass:function(){//获取类名
return this._type;
},
inited:false,//已经初始化标志
Public:function(obj){//公共作用域
$util.extend(this.constructor.prototype,obj,true,'public');
if(!this.inited){
$util.extend(this,obj,true);
}
},
Private:function(obj){//私有作用域
$util.extend(this.constructor.prototype,obj,true,'private');
if(!this.inited){
$util.extend(this,obj,true);
}
},
classend:function(){//类定义结束符
this.inited = true;
}
},true);
//基类模板加载
new Class();
//=============================Utilities END=========================
//=============================Prototype=========================
//为函数添加作用域
$util.extend(Function.prototype,{
scope:"public"
});

//为对象添加继承方法
$util.extend(Object.prototype,{
Extends:function(parent){
if(!parent){
parent = Class;
}
if(!$util.classExist(parent)){
new parent();
}
if(!this.inited){
var reg = /function\s([a-zA-z]+)\(/;
this._type=this.constructor.prototype._type=this.constructor.toString().match(reg)[1];
$util.inherit(this,parent);
}
}
});
//=============================Prototype END========================
//=============================Test========================
function A(){
this.Extends(Class);//继承自基类
//公共属性和方法
this.Public({
test:function(){
alert('test');
}
});
//私有属性和方法
this.Private({
test1:function(){
alert('test1');
}
});
this.classend();//类定义结束
}

function B(){
this.Extends(A);//继承自A类
this.classend();//类定义结束
}

new B().test();
//=============================Test END========================
wfyfjplfjj 2010-10-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zjhiphop2006 的回复:]

不错,关注一下!不知道楼主是出于什么样的想法要做这些封装呢?
[/Quote]

想实现一个类java的基本架构,支持作用域private,public,支持父类访问,支持接口,暂时向这个目标努力了
wfyfjplfjj 2010-10-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zjhiphop2006 的回复:]

public和extends在js中指关键字,楼主需要修改一下封装,如下:
JScript code
var $util = new (function(){
//指定class是否已加载
this.classExist = function(clazz){
return !(!clazz.prototype.type);
}
//扩展……
[/Quote]
谢谢了,之前的Firefox中试过那个没有问题,不过今天起来还是改过了,我把它改成$public和$private,$super了,不过你的更加直观一点,谢谢了
zjhiphop2006 2010-10-23
  • 打赏
  • 举报
回复
不错,关注一下!不知道楼主是出于什么样的想法要做这些封装呢?
kahnnash 2010-10-23
  • 打赏
  • 举报
回复
JavaScript不太懂啊!!
wfyfjplfjj 2010-10-23
  • 打赏
  • 举报
回复
晚上回来又续写了,不重新开贴了就,功能一点点加,现在的父类调用还存在问题

var $util = new (function(){
//指定class是否已加载
this.classExist = function(clazz){
return !(!clazz.prototype.type);
}
//扩展
this.extend = function (dest,src,force,scope,filter){
for(var name in src){
if(!force && dest[name]){
continue;
}
if(scope){
src[name].scope = scope;
}
if(filter){
if(!filter(src[name])){
continue;
}
}
dest[name] = src[name];
}
};
//继承
this.inherit = function(current,parent){
this.extend(current,parent.prototype,true,null,function(obj){
return obj.scope === "public";
});
current.constructor.prototype = current;
};
//遍历一个对象中的属性,调试用
this.iterateTest = function(obj){
for(var name in obj){
alert(name);
}
};
//判断是否为函数
this.isFn = function(obj){
return typeof(obj) === "function";
};
})();
//基类型
function Class(){}
//为基类型添加属性和方法
$util.extend(Class.prototype,{
_type:"Class",//类名
getClass:function(){//获取类名
return this._type;
},
inited:false,//已经初始化标志
public:function(obj){//公共作用域
$util.extend(this.constructor.prototype,obj,true,'public');
if(!this.inited){
$util.extend(this,obj,true);
}
},
private:function(obj){//私有作用域
$util.extend(this.constructor.prototype,obj,true,'private');
if(!this.inited){
$util.extend(this,obj,true);
}
},
classend:function(){//类定义结束符
this.inited = true;
}
},true);
//基类模板加载
new Class();
//=============================Utilities END=========================
//=============================Prototype=========================
//为函数添加作用域
$util.extend(Function.prototype,{
scope:"public"
});

//为对象添加继承方法
$util.extend(Object.prototype,{
extends:function(parent){
if(!parent){
parent = Class;
}
if(!$util.classExist(parent)){
new parent();
}
if(!this.inited){
var reg = /function\s([a-zA-z]+)\(/;
this._type=this.constructor.prototype._type=this.constructor.toString().match(reg)[1];
$util.inherit(this,parent);
}
}
});
//=============================Prototype END========================
//=============================Test========================
function A(){
this.extends(Class);//继承自基类
//公共属性和方法
this.public({
test:function(){
alert('test');
}
});
//私有属性和方法
this.private({
test1:function(){
alert('test1');
}
});
this.classend();//类定义结束
}

function B(){
this.extends(A);//继承自A类
this.classend();//类定义结束
}

new B().test();
//=============================Test END========================
大纲007 2010-10-23
  • 打赏
  • 举报
回复
开眼了,谢谢!
ajccom 2010-10-23
  • 打赏
  • 举报
回复
还是对可视化的具象化的比较有爱啊
jsj_126abc 2010-10-23
  • 打赏
  • 举报
回复
在公司里一般搞软件开发的做些什么事呢,
真哥哥 2010-10-23
  • 打赏
  • 举报
回复

wfyfjplfjj 2010-10-23
  • 打赏
  • 举报
回复
刚才发现一点问题,super的封装应该这样就OK了,不然super为空

var args = Array.prototype.slice.apply(arguments);
this._super.apply(this.super,args);
//$.iteratorTest(this.super);
$.extend(this,this.super,true);
wfyfjplfjj 2010-10-23
  • 打赏
  • 举报
回复
在公司闲来无事搞的,才开始写一点点
小辛、 2010-10-23
  • 打赏
  • 举报
回复
仔细研究一下!~~~~~~~~~ 楼主好心情哇!~~~~~~~~~
caizhenyu888 2010-10-23
  • 打赏
  • 举报
回复
头一次 ⊙﹏⊙

87,918

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧