月影:function扩展
作者:月影 来源:无忧脚本 时间:2008-05-19 12:27:00
<script>
Function.prototype.$bind=function(object)
{
var callback = function (fn) {
return fn;
}
with(object)
{
return eval('callback(' + this.toString() + ')');
}
}
var obj={a:1,b:2};
var f=function (){
a=10;
b=11;
}.$bind(obj);
f();
alert(obj.a);
//----------------------------------------------------------------------------------------------------
G = {};
G.objInstanceOf = function(obj, c){
if(typeof(c) == "string")
return typeof(obj) == c;
if(obj instanceof c || c.__templete__ && obj instanceof c.__templete__)
return true;
var _interfaces = obj && obj.__interfaces__;
if(_interfaces)
for(var i = 0, len = _interfaces; i < len; i++){
if(_interfafces[i] == c)
return true;
}
return false;
};
G.objectAsPrototype = function(obj, c){
c = c || function(){};
c.prototype = obj;
return c;
};
Function.prototype.getPrototypeObject = function(){
var p = this.__templete__ || (this.__templete__= G.objectAsPrototype(this.prototype));
return new p();
};
Function.prototype.$pextends = function(p){
var me = this.$bind({$super:p});
var ins = function()
{
var ret = me.apply(this, arguments);
return ret;
}
ins.prototype = p.getPrototypeObject();
return ins;
};
//--------------------------------------------------------------------------------------------------------
function B(){};
var A = function(){
alert($super);
}.$pextends(B);
var a = new A();
//---------------------------------------------------------------------------------------------------------
Function.prototype.$verify = function(){
var me = this;
var _args = arguments;
//第一范式 [new] T <=> [new] R:function(){donothing, return T.apply},R.prototype = T.prototype
var mins = function(){
for(var i = 0, len = _args.length; i < len; i++)
{
if(!G.objInstanceOf(arguments[i],_args[i])){
throw new TypeError("函数的参数类型不匹配,位置:"+(i+1));
}
}
return me.apply(this, arguments);
}
mins.prototype = me.prototype;
return mins;
}
//-------------------------------------------------------------------------------------------------------
var foo = function(x,y){
alert(x+y);
}.$verify("number","number");
foo(1,2);
var foo2 = function(x,y){
x(y);
}.$verify(Function,"number");
//foo("error",2);
foo2(function(x){alert(x)},10);
//foo2("x","y");
var Class3 = function(x,y){
this.x = x;
this.y = y;
}.$verify("number","number");
Class3.prototype.dist2 = function(){return this.x*this.x + this.y*this.y};
var c = new Class3(10,20);
alert(c.dist2());
//--------------------------------------------------------------------------------------------------------
Function.prototype.$staticable = function()
{
var me = this;
var cache = [];
var index = 0;
var mins = function(){
mins.alloc = function(){
cache[index] = cache[index] || {};
return cache[index++];
};
var ret = me.apply(this, arguments);
mins.alloc = null;
index = 0;
return ret;
}
mins.prototype = me.prototype;
return mins;
}
//----------------------------------------------------------------------------------------------------------
var Test = function(){
var x = Test.alloc();
x.a = x.a || 0;
x.a++;
return x.a;
}.$staticable();
for(var i = 0; i < 10; i++)
alert(Test());
//-----------------------------------------------------------------------------------------------------------
Function.prototype.$protected = function(){
var me = this;
var mins = function(){
var p = me.getPrototypeObject();
var ret = function(){}; //create a new object
for(var each in mins.prototype) //clone prototypes
{
if(mins.prototype[each] instanceof Function){
ret.prototype[each] = function(){
return mins.prototype[each].apply(p,arguments); //call by p
}
p[each] = mins.prototype[each];
}
else{
p[each] = ret.prototype[each] = mins.prototype[each];
}
}
me.apply(p,arguments);//clone a new object by me
return new ret(); //return this object;
}
return mins;
}
//----------------------------------------------------------------------------------------------------------------
//将一个类型的this域声明为全部私有
var Test = function(x,y)
{
this.x = x;
this.y = y;
}.$protected();
Test.prototype.getX = function(){
return this.x;
}
var t = new Test(5,10);
alert(t.x);
alert(t.getX());
</script>
标签:function,扩展,js
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server 2005数据转换服务设计问题集锦
2008-12-26 17:29:00
发一新浪招聘的图片滚动控制JS效果
2011-08-10 19:17:25
利用FrontPage 2003制作网络申请系统
2008-02-21 14:34:00
![](https://img.aspxhome.com/file/uploadpic/20082/21/2008221143853265.jpg)
MySQL数据库中的重要数据应当如何保护
2008-12-17 15:56:00
asp在sql server2000中新建帐号和给帐号权限代码
2008-01-29 13:46:00
讲解数据库加密技术的功能特性与实现方法
2008-12-18 14:24:00
XML入门的常见问题(一)
2008-09-05 17:20:00
一位网友的DIV CSS编码笔记——XieBiji
2008-09-12 12:22:00
linux下如何备份还原mysql数据库
2010-08-08 08:59:00
通过MySQL内置全文检索实现中文的相关检索
2010-06-11 13:20:00
Dreamweaver MX网页图片热区使用方法
2008-05-20 12:50:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113613945s.jpg)
SQL Server 中死锁产生的原因及解决办法
2008-11-25 11:50:00
IE7下动态创建Iframe时,去除边框的办法
2009-01-19 13:56:00
成为一个顶级设计师的第一准则
2008-04-18 10:29:00
![](https://img.aspxhome.com/file/UploadPic/20084/18/2008418103351248s.jpg)
ASP自动解压RAR文件代码
2007-11-06 13:29:00
WEB2.0网页制作标准教程(4)如何调用css样式表
2007-11-13 13:26:00
随感:交互设计两三事
2009-05-18 13:54:00
![](https://img.aspxhome.com/file/UploadPic/20095/18/e59bbe-42s.jpg)
利用xmlhttp和adodb.stream加缓存技术下载远程Web文
2009-04-23 18:33:00
说说掌握JavaScript语言的思想前提
2008-12-26 17:59:00
ASP网站数据采集经验谈
2008-03-09 15:30:00