月影: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
0
投稿
猜你喜欢
聊聊MySQL中的存储引擎
2024-01-20 08:58:11
在Python的web框架中配置app的教程
2023-03-28 12:12:17
pycharm2020.2 配置使用的方法详解
2022-10-01 12:23:23
Python中的 enum 模块源码详析
2021-11-19 04:11:10
Java基于redis和mysql实现简单的秒杀(附demo)
2024-01-16 16:55:13
Windows下使Python2.x版本的解释器与3.x共存的方法
2021-03-14 22:22:17
PHP读取文本文件并逐行输出该行使用最多的字符与对应次数的方法
2024-05-11 10:09:43
asp下通过HTTP_USER_AGENT判断用户是从手机上访问,还是电脑IE上访问
2011-02-24 11:00:00
在查询分析器理启动或停止SQL Agent服务
2009-01-08 16:20:00
Python使用arrow库优雅地处理时间数据详解
2023-02-26 07:14:12
goland -sync/atomic原子操作小结
2024-04-26 17:20:08
详解Mysql自动备份与恢复的几种方法(图文教程)
2024-01-20 20:05:06
pycharm 2019 最新激活方式(pycharm破解、激活)
2021-09-15 13:07:01
详解MySql的慢查询分析及开启慢查询日志
2024-01-18 12:56:56
解决usageerror: line magic function "%%time" not found问题
2022-06-16 15:53:29
Python的@property的使用
2021-03-27 11:02:25
ajax+asp无限级分类树型结构
2011-04-02 11:05:00
rs.open与conn.execute详细解释
2008-07-03 12:54:00
textarea 在浏览器中固定大小和禁止拖动的实现方法
2024-04-19 10:17:41
JavaScript 函数惰性载入的实现及其优点介绍
2024-04-16 09:25:37