月影:function扩展
作者:月影 来源:无忧脚本 发布时间:2008-05-19 12:27:00
标签:function,扩展,js
<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>


猜你喜欢
- 为什么要同步SQL Server 2000 数据库,它都用在什么场合SQL Server 2000 数据库同步配置的原理从0开始一步一步配置
- 本文实例讲述了python实现的简单抽奖系统。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/env python#codin
- 菜单栏,tools--去掉勾选的Vim Emulator这个仿真插件就好了。来源:https://blog.csdn.net/weixin_
- Go提供几种方法检查变量的类型,在字符串格式化标识%T, 反射方式:reflect.TypeOf, reflect.ValueOf.Kind
- conn.execute、rs.open之间的差别,conn.execute、rs.open、command.execute方法用法大大不同
- 一、中间件的基本使用在web开发中,中间件起着很重要的作用。比如,身份验证、权限认证、日志记录等。以下就是各框架对中间件的基本使用。1.1
- 本文实例讲述了python通过ssh-powershell监控windows的方法。分享给大家供大家参考。具体分析如下:对于服务器的监控来说
- 本文实例讲述了php查找指定目录下指定大小文件的方法。分享给大家供大家参考。具体实现方法如下:php查找文件大小的原理是遍历目录然后再利用f
- 本文介绍了深入理解ES6的迭代器与生成器,分享给大家,具体如下:循环语句的问题var colors = ["red",
- 对于Linux用户来说,命令行的名声相当的高。不像其他操作系统,命令行是一个可怕的命题,但是对于Linux社区中那些经验丰富的大牛,命令行却
- 数据库:保存图片的数据格式 图象二进制数据储存字段前台: <%@ Page Language="C#" AutoE
- 本文实例为大家分享了python创建tcp服务端和客户端的具体代码,供大家参考,具体内容如下1.服务端serverfrom socket i
- 转用一门新语言通常是一项大决策,尤其是当你的团队成员中只有一个使用过它时。今年 Stream 团队的主要编程语言从 Python 转向了 G
- 本文实例为大家分享了GO原生实现文件上传功能的具体代码,供大家参考,具体内容如下写在前面最近在学习go,发现实践才是检验真理的唯一标准。在不
- Python的3.0版本,常被称为Python 3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。为了不带入过多的
- 代码如下:<% function CheckFileContent(FileName) dim 
- 以前有过一篇类似的文章, 讲的比较简单只有三个插件而已, 所以这篇文章将全持续更新.jQuery UI 大名顶顶, 不用介绍, 它的各个控件
- 糟糕的SQL查询语句可对整个应用程序的运行产生严重的影响,其不仅消耗掉更多的数据库时间,且它将对其他应用组件产生影响。如同其它学科,优化查询
- 与Channel区别Channel能够很好的帮助我们控制并发,但是在开发习惯上与显示的表达不太相同,所以在Go语言中可以利用sync包中的W
- 概述Python是个非常受欢迎的编程语言,随着近些年机器学习、云计算等技术的发展,Python的职位需求越来越高。下面我收集了10个Pyth