- 浏览: 18778 次
- 性别:
- 来自: 珠海
最近访客 更多访客>>
文章分类
最新评论
-
ajaxgo:
02221021 写道楼主, Object上挂这么多方法不合适 ...
jsadk core 发布——论坛建成www.jsadk.com -
ajaxgo:
vb2005xu 写道一个JS核心包竟然有8M 我日 你真强
...
jsadk core 发布——论坛建成www.jsadk.com -
02221021:
楼主, Object上挂这么多方法不合适吧, 怎么不自定义一个 ...
jsadk core 发布——论坛建成www.jsadk.com -
vb2005xu:
一个JS核心包竟然有8M 我日 你真强
jsadk core 发布——论坛建成www.jsadk.com -
ajaxgo:
calmness 写道sonicluo3 写道希望可以做到模块 ...
『原创』基础型js库 jsadk v0.1a 发布
首先很感谢bingo,用非常新颖的思路,实现了这么一个js的loader工具。往外抛异常阻止函数运行,并在加载完后继续运行caller的做法,让人眼前一亮。不得不佩服js的世界是如此的奇妙。
在阅读bingo的js loader,获得启发的同时,也对其中的代码功能做了一些调整。例如,添加了绑定事件机制,可以预先绑定需要在加载(loaded)完后触发的操作(事件一共有5类)。规范了消息队列的实现,bingo的实现中,把消息队列耦合的太紧密。所以独立实现了一个消息队列,在loader中使用等等。整个代码经过重写,并加了注释,方便爱好者学习和探讨,也希望大家反馈bug。并且允许在include时,设置caller的scope和arguments,这样就可以在一般的函数中,比如实例方法(而不只是在匿名函数)使用include。因为,bingo的实现中,在调用caller时,只是简单的运行caller,如果在有需要作用域和需要参数传递的函数中,会失去原本的作用域和传递进来的参数。
最后,还是非常感谢bingo!
代码如下(代码下载在最后):
js 代码
- var jsLoader=new function() {
- /*
- * const
- */
- this.NAMESPACE_TYPE={
- CLASS:'class',
- INTERFACE:'interface',
- OBJECT:'object'
- };
- this.LOADMODE_TYPE={
- PACKAGE:'package',
- FILE:'file',
- DYNAMIC:'dynamic'
- };
- this.ERROR_LEVEL={
- INFO:'@jsadk:',
- DEBUG:'*@jsadk:',
- FATAL:'!*@jsadk:',
- THROWS:'^!*@jsadk:'
- };
- this.ERRORS={};
- /*
- * private member
- */
- var _this=this;
- var _version='1.0a';
- var _libPath='./';
- var _errLv=this.ERROR_LEVEL.FATAL;
- var _loadmode=this.LOADMODE_TYPE.PACKAGE;
- var _packages={};
- /*
- * getter and setter
- */
- this.getVersion=function() {
- return _version;
- };
- this.getLibPath=function() {
- return _libPath;
- };
- this.setLibPath=function(libPath) {
- _libPath=libPath;
- };
- this.getLoadMode=function() {
- return _loadmode;
- };
- this.setLoadMode=function(loadmode) {
- _loadmode=loadmode;
- };
- this.getErrLevel=function() {
- return _errLv;
- }
- this.setErrLevel=function(level) {
- _errLv=level;
- };
- /*
- * register packages infos
- */
- /**
- * 增加包信息
- * @param {Object} root
- * @param {Hash} pks
- */
- var _addPKGs=function(root,pks) {
- for (var pk in pks) {
- if (typeof pks[pk]=='string') {
- root[pk]=_this.Util.initVar(root[pk],pks[pk]);
- } else if(typeof pks[pk]=='object') {
- root[pk]=_this.Util.initVar(root[pk],{});
- _addPKGs(root[pk],pks[pk]);
- }
- }
- };
- /**
- * 获得注册的包信息
- * @param {String|Object} pack
- */
- this.getPKGs=function(pack) {
- return _this.Util.eval(pack,_packages);
- };
- /**
- * 注册包信息
- * @param {Hash} pkgs
- * @param {String} ns
- */
- this.regPKGs=function(pkgs,ns) {
- var size=arguments.length;
- var ns=ns.split('.');
- var root=_packages;
- for (var i=0;i
- root=root[ns[i]]=_this.Util.initVar(root[ns[i]],{});
- }
- _addPKGs(root,pkgs);
- };
- /*
- * catch the window error
- */
- window.onerror=function() {
- if (arguments[0].indexOf(_errLv)>-1) {
- return false;
- } else {
- return true;
- }
- };
- };
- with(jsLoader) {
- /*
- * utility methods
- */
- jsLoader.Util={
- /**
- * 一个空函数的引用
- */
- emptyFunction:function() {},
- /**
- * 在给定的scope中执行代码
- * @param {String} obj
- * @param {Object} scope[default=window]
- */
- eval:function(obj,scope) {
- scope=(scope==null)?window:scope;
- if (typeof obj=='string') {
- try {
- with(scope) {
- obj=eval(obj);
- }
- } catch (e) {
- return undefined;
- }
- }
- return obj;
- },
- /**
- * 检查在给定的scope中对象是否存在
- * @param {String|Object} obj
- * @param {Object} scope[default=window]
- */
- exist:function(obj,scope) {
- return (typeof this.eval(obj,scope)=='undefined'?false:true);
- },
- /**
- * 用给定的值initVal,初始化变量obj,并返回obj。如果obj不为undefined,则直接返回obj
- * @param {Object} obj
- * @param {Object} initVal
- */
- initVar:function(obj,initVal) {
- if (typeof obj=='undefined') {
- obj=initVal;
- }
- return obj;
- },
- /**
- * 用source扩展destination,并返回destination。
- * fProperty和fValue用来筛选source中的属性和值
- * @param {Object} destination
- * @param {Object} source
- * @param {Function} fProperty(property,destination)[scope=source]
- * @param {Function} fValue(value,property,destination)[scope=source]
- */
- extend:function(destination,source,fProperty,fValue) {
- for (var property in source) {
- var _property=(fProperty || Util.emptyFunction).call(source,property,destination);
- if (_property!=false) {
- var _value=(fValue!=null?
- fValue.call(source,source[property],property,destination):
- source[property]);
- destination[typeof _property=='string'?_property:property]=_value;
- }
- }
- return destination;
- }
- };
- /*
- * extends Array
- */
- (function() {
- Util.extend(Array.prototype,{
- first:function() {
- return this[0];
- },
- last:function() {
- return this[this.length-1];
- },
- /**
- * 给出element在队列中所处的位置索引(第一个),如果不存在则返回-1。参数struct为true是进行严格比较
- * @param {Object} element
- * @param {Boolean} struct[default=false]
- */
- indexOf:function(element,struct) {
- var size=this.length;
- for (var i=0;i
- if ((struct && this[i]===element) ||
- (!struct && this[i]==element)) {
- break;
- }
- }
- return (i==size?-1:i);
- },
- /**
- * 在索引index处插入元素element1,element2,....
- * @param {Number} index
- */
- insertAt:function(index) {
- var args=[index,0];
- var size=arguments.length;
- for (var i=1;i
- args.push(arguments[i]);
- }
- this.splice.apply(this,args);
- },
- /**
- * 删除索引index处的size个元素,并返回删除的元素数组
- * @param {Number} index
- * @param {Number} size[opt=1]
- */
- deleteAt:function(index,size) {
- result=this.splice(index,size || 1);
- return result;
- },
- /**
- * 清空队列
- */
- clear:function() {
- this.length=0;
- }
- });
- })();
- /**
- * 事件队列,用于注册和注销事件。并触发对应事件
- */
- jsLoader.EventQueue=(function() {
- Util.extend(ERRORS,{
- 'EventQueue.NO_SUCH_EVENT':ERROR_LEVEL.DEBUG+'no such a event',
- 'EventQueue.ALEADY_EXIST':ERROR_LEVEL.DEBUG+'the message is already exsited!'
- });
- var constructor=function(event) {
- this._queue={};
- this.EVENT={};
- for (var e in event) {
- this.EVENT[event[e]]=true;
- this._queue[event[e]]=[];
- }
- };
- Util.extend(constructor.prototype,{
- /**
- * 触发队列中,对应事件event的消息。参数dispose为ture时,将在触发完后删除该事件的所有消息。
- * @param {String} event
- * @param {Boolean} dispose[default=false]
- * @param {Array} args[default=[]]
- */
- dispatch:function(event,dispose,args) {
- if (this.EVENT[event]) {
- var messages=this._queue[event];
- var size=messages.length;
- try {
- for (var i=0;i
- messages[i++].apply(messages[i++],messages[i++].concat(args || []));
- }
- } catch(e) {
- throw new Error(ERROR_LEVEL.DEBUG+e.message);
- } finally {
- if (dispose) {
- this._queue[event].clear();
- }
- }
- } else {
- throw new Error(ERRORS['EventQueue.NO_SUCH_EVENT']+':'+event);
- }
- },
- /**
- * 注册事件event的消息message。scope和args参数指定了消息调用时的作用域和参数。
- * @param {String} event
- * @param {Function} message
- * @param {Object} scope[default=window]
- * @param {Array} args[default=[]]
- */
- on:function(event,message,scope,args) {
- if (this.EVENT[event]) {
- if (this._queue[event].indexOf(message)==-1) {
- this._queue[event].push(message);
- this._queue[event].push(scope || window);
- this._queue[event].push(args || []);
- } else {
- throw new Error(ERRORS['EventQueue.ALEADY_EXIST']+':'+event);
- }
- } else {
- throw new Error(ERRORS['EventQueue.NO_SUCH_EVENT']+':'+event);
- }
- },
- /**
- * 注销事件event的小心message
- * @param {String} event
- * @param {Function} message
- */
- un:function(event,message) {
- var index;
- if (this.EVENT[event] && (index=this._queue[event].indexOf(message))!=-1) {
- this._queue[event].deleteAt(index,3);
- } else {
- throw new Error(ERRORS['EventQueue.NO_SUCH_EVENT']+':'+event);
- }
- }
- });
- return constructor;
- })();
- /*
- * a class of message queue, used to invoke messages orderly
- */
- jsLoader.MessageQueue=(function() {
- var constructor=function() {
- this._queue=[];
- this._listened=null;
- }
- Util.extend(constructor.prototype,{
- /**
- * 队列监听器,识别队列中是否有可以产生的消息
- */
- _listen:function() {
- var _this=this;
- if (this._listened) {
- if (this._queue.length==0) {
- setTimeout(function() {
- _this._listen()
- },500);
- } else {
- this.inform();
- }
- }
- },
- /**
- * 启动监听器
- */
- startListen:function() {
- this._listened=true;
- this._listen();
- },
- /**
- * 停止监听器
- */
- stopListen:function() {
- this._listened=false;
- },
- /**
- * 通知消息队列,另起产生队列头部的一个消息
- */
- inform:function() {
- if (this._queue.length==0) {
- this._listen();
- return;
- } else {
- var message=this._queue.shift();
- var scope=this._queue.shift();
- var args=this._queue.shift();
- try {
- JsLoader.rar (7 KB)
- 下载次数: 256
评论
7 楼
rain16881
2008-04-21
我用了之后是可以按自己想的顺序加载了..
不过.性能是一个好大的问题.
加上..我在js 的有调用的..一个也不要用.
不过.性能是一个好大的问题.
加上..我在js 的有调用的..一个也不要用.
6 楼
rain16881
2008-04-21
有的ext desktop就有一个js加载顺序的问题.
希望能用你的js loader解决
希望能用你的js loader解决
5 楼
brull
2008-01-14
<div class='quote_title'>ajaxgo 写道</div><div class='quote_div'><div class='quote_title'>brull 写道</div><div class='quote_div'><div class='quote_div'>呵呵,原来在12月份就有这么个东西发布了呢,看样子不错,不过代码没仔细看。对于bingo我也很长时间没空去进一步完善了,希望来年来完善一下。冒味问下,你是与狼共舞?</div></div><p> </p><p>我不是与狼共舞哦,呵呵,不好意思</p></div><br/>那看来对这个东西感兴趣的人还是有一些的,我也感到比较欣慰,再接再厉
4 楼
ajaxgo
2008-01-14
<div class='quote_title'>brull 写道</div><div class='quote_div'><div class='quote_div'>呵呵,原来在12月份就有这么个东西发布了呢,看样子不错,不过代码没仔细看。对于bingo我也很长时间没空去进一步完善了,希望来年来完善一下。冒味问下,你是与狼共舞?</div></div><p> </p><p>我不是与狼共舞哦,呵呵,不好意思</p>
3 楼
ajaxgo
2008-01-14
sun2grit 写道
能举几个简单的调用示例吗?
下载的代码里面有示例
2 楼
brull
2008-01-12
<div class='quote_title'>ajaxgo 写道</div><div class='quote_div'><p>首先很感谢bingo,用非常新颖的思路,实现了这么一个js的loader工具。往外抛异常阻止函数运行,并在加载完后继续运行caller的做法,让人眼前一亮。不得不佩服js的世界是如此的奇妙。</p><p>在阅读bingo的js loader,获得启发的同时,也对其中的代码功能做了一些调整。例如,添加了绑定事件机制,可以预先绑定需要在加载(loaded)完后触发的操作(事件一共有5类)。规范了消息队列的实现,bingo的实现中,把消息队列耦合的太紧密。所以独立实现了一个消息队列,在loader中使用等等。整个代码经过重写,并加了注释,方便爱好者学习和探讨,也希望大家反馈bug。并且允许在include时,设置caller的scope和arguments,这样就可以在一般的函数中,比如实例方法(而不只是在匿名函数)使用include。因为,bingo的实现中,在调用caller时,只是简单的运行caller,如果在有需要作用域和需要参数传递的函数中,会失去原本的作用域和传递进来的参数。</p><p>最后,还是非常感谢bingo!</p><p>代码如下(代码下载在最后):</p><div class='code_title'>js 代码</div><div class='dp-highlighter'><ol class='dp-c'><li class='alt'><span><span> </span></span> </li><li><span class='keyword'>var</span><span> jsLoader=</span><span class='keyword'>new</span><span> </span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> </span><span class='comment'>/* </span> </li><li><span><span class='comment'> * const </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> </span><span class='keyword'>this</span><span>.NAMESPACE_TYPE={ </span> </li><li class='alt'><span> CLASS:'</span><span class='keyword'>class</span><span>', </span> </li><li><span> INTERFACE:'</span><span class='keyword'>interface</span><span>', </span> </li><li class='alt'><span> OBJECT:'object' </span> </li><li><span> }; </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>.LOADMODE_TYPE={ </span> </li><li><span> PACKAGE:'</span><span class='keyword'>package</span><span>', </span> </li><li class='alt'><span> FILE:'file', </span> </li><li><span> DYNAMIC:'dynamic' </span> </li><li class='alt'><span> }; </span> </li><li><span> </span><span class='keyword'>this</span><span>.ERROR_LEVEL={ </span> </li><li class='alt'><span> INFO:'@jsadk:', </span> </li><li><span> DEBUG:'*@jsadk:', </span> </li><li class='alt'><span> FATAL:'!*@jsadk:', </span> </li><li><span> THROWS:'^!*@jsadk:' </span> </li><li class='alt'><span> }; </span> </li><li><span> </span><span class='keyword'>this</span><span>.ERRORS={}; </span> </li><li class='alt'><span> </span><span class='comment'>/* </span> </li><li><span><span class='comment'> * private member </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> </span><span class='keyword'>var</span><span> _this=</span><span class='keyword'>this</span><span>; </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> _version='1.0a'; </span> </li><li><span> </span><span class='keyword'>var</span><span> _libPath='./'; </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> _errLv=</span><span class='keyword'>this</span><span>.ERROR_LEVEL.FATAL; </span> </li><li><span> </span><span class='keyword'>var</span><span> _loadmode=</span><span class='keyword'>this</span><span>.LOADMODE_TYPE.PACKAGE; </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> _packages={}; </span> </li><li><span> </span><span class='comment'>/* </span> </li><li class='alt'><span><span class='comment'> * getter and setter </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>.getVersion=</span><span class='keyword'>function</span><span>() { </span> </li><li><span> </span><span class='keyword'>return</span><span> _version; </span> </li><li class='alt'><span> }; </span> </li><li><span> </span><span class='keyword'>this</span><span>.getLibPath=</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span> _libPath; </span> </li><li><span> }; </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>.setLibPath=</span><span class='keyword'>function</span><span>(libPath) { </span> </li><li><span> _libPath=libPath; </span> </li><li class='alt'><span> }; </span> </li><li><span> </span><span class='keyword'>this</span><span>.getLoadMode=</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span> _loadmode; </span> </li><li><span> }; </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>.setLoadMode=</span><span class='keyword'>function</span><span>(loadmode) { </span> </li><li><span> _loadmode=loadmode; </span> </li><li class='alt'><span> </span> </li><li><span> }; </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>.getErrLevel=</span><span class='keyword'>function</span><span>() { </span> </li><li><span> </span><span class='keyword'>return</span><span> _errLv; </span> </li><li class='alt'><span> } </span> </li><li><span> </span><span class='keyword'>this</span><span>.setErrLevel=</span><span class='keyword'>function</span><span>(level) { </span> </li><li class='alt'><span> _errLv=level; </span> </li><li><span> }; </span> </li><li class='alt'><span> </span><span class='comment'>/* </span> </li><li><span><span class='comment'> * register packages infos </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 增加包信息 </span> </span> </li><li><span><span class='comment'> * @param {Object} root </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Hash} pks </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> _addPKGs=</span><span class='keyword'>function</span><span>(root,pks) { </span> </li><li><span> </span><span class='keyword'>for</span><span> (</span><span class='keyword'>var</span><span> pk </span><span class='keyword'>in</span><span> pks) { </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>typeof</span><span> pks[pk]=='string') { </span> </li><li><span> root[pk]=_this.Util.initVar(root[pk],pks[pk]); </span> </li><li class='alt'><span> } </span><span class='keyword'>else</span><span> </span><span class='keyword'>if</span><span>(</span><span class='keyword'>typeof</span><span> pks[pk]=='object') { </span> </li><li><span> root[pk]=_this.Util.initVar(root[pk],{}); </span> </li><li class='alt'><span> _addPKGs(root[pk],pks[pk]); </span> </li><li><span> } </span> </li><li class='alt'><span> } </span> </li><li><span> }; </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 获得注册的包信息 </span> </span> </li><li class='alt'><span><span class='comment'> * @param {String|Object} pack </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>.getPKGs=</span><span class='keyword'>function</span><span>(pack) { </span> </li><li><span> </span><span class='keyword'>return</span><span> _this.Util.eval(pack,_packages); </span> </li><li class='alt'><span> }; </span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 注册包信息 </span> </span> </li><li><span><span class='comment'> * @param {Hash} pkgs </span> </span> </li><li class='alt'><span><span class='comment'> * @param {String} ns </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>.regPKGs=</span><span class='keyword'>function</span><span>(pkgs,ns) { </span> </li><li><span> </span><span class='keyword'>var</span><span> size=arguments.length; </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> ns=ns.split(</span><span class='string'>'.'</span><span>); </span> </li><li><span> </span><span class='keyword'>var</span><span> root=_packages; </span> </li><li class='alt'><span> </span><span class='keyword'>for</span><span> (</span><span class='keyword'>var</span><span> i=0;i </span></li><li><span> root=root[ns[i]]=_this.Util.initVar(root[ns[i]],{}); </span> </li><li class='alt'><span> } </span> </li><li><span> _addPKGs(root,pkgs); </span> </li><li class='alt'><span> }; </span> </li><li><span> </span><span class='comment'>/* </span> </li><li class='alt'><span><span class='comment'> * catch the window error </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> window.onerror=</span><span class='keyword'>function</span><span>() { </span> </li><li><span> </span><span class='keyword'>if</span><span> (arguments[0].indexOf(_errLv)>-1) { </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span> </span><span class='keyword'>false</span><span>; </span> </li><li><span> } </span><span class='keyword'>else</span><span> { </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span> </span><span class='keyword'>true</span><span>; </span> </li><li><span> } </span> </li><li class='alt'><span> }; </span> </li><li><span>}; </span> </li><li class='alt'><span> </span> </li><li><span class='keyword'>with</span><span>(jsLoader) { </span> </li><li class='alt'><span> </span><span class='comment'>/* </span> </li><li><span><span class='comment'> * utility methods </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> jsLoader.Util={ </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 一个空函数的引用 </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> emptyFunction:</span><span class='keyword'>function</span><span>() {}, </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 在给定的scope中执行代码 </span> </span> </li><li class='alt'><span><span class='comment'> * @param {String} obj </span> </span> </li><li><span><span class='comment'> * @param {Object} scope[default=window] </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> eval:</span><span class='keyword'>function</span><span>(obj,scope) { </span> </li><li class='alt'><span> scope=(scope==</span><span class='keyword'>null</span><span>)?window:scope; </span> </li><li><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>typeof</span><span> obj=='string') { </span> </li><li class='alt'><span> </span><span class='keyword'>try</span><span> { </span> </li><li><span> </span><span class='keyword'>with</span><span>(scope) { </span> </li><li class='alt'><span> obj=eval(obj); </span> </li><li><span> } </span> </li><li class='alt'><span> } </span><span class='keyword'>catch</span><span> (e) { </span> </li><li><span> </span><span class='keyword'>return</span><span> undefined; </span> </li><li class='alt'><span> } </span> </li><li><span> } </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span> obj; </span> </li><li><span> }, </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 检查在给定的scope中对象是否存在 </span> </span> </li><li class='alt'><span><span class='comment'> * @param {String|Object} obj </span> </span> </li><li><span><span class='comment'> * @param {Object} scope[default=window] </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> exist:</span><span class='keyword'>function</span><span>(obj,scope) { </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span> (</span><span class='keyword'>typeof</span><span> </span><span class='keyword'>this</span><span>.eval(obj,scope)=='undefined'?</span><span class='keyword'>false</span><span>:</span><span class='keyword'>true</span><span>); </span> </li><li><span> }, </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 用给定的值initVal,初始化变量obj,并返回obj。如果obj不为undefined,则直接返回obj </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Object} obj </span> </span> </li><li><span><span class='comment'> * @param {Object} initVal </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> initVar:</span><span class='keyword'>function</span><span>(obj,initVal) { </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>typeof</span><span> obj=='undefined') { </span> </li><li><span> obj=initVal; </span> </li><li class='alt'><span> } </span> </li><li><span> </span><span class='keyword'>return</span><span> obj; </span> </li><li class='alt'><span> }, </span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 用source扩展destination,并返回destination。 </span> </span> </li><li><span><span class='comment'> * fProperty和fValue用来筛选source中的属性和值 </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Object} destination </span> </span> </li><li><span><span class='comment'> * @param {Object} source </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Function} fProperty(property,destination)[scope=source] </span> </span> </li><li><span><span class='comment'> * @param {Function} fValue(value,property,destination)[scope=source] </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> extend:</span><span class='keyword'>function</span><span>(destination,source,fProperty,fValue) { </span> </li><li class='alt'><span> </span><span class='keyword'>for</span><span> (</span><span class='keyword'>var</span><span> property </span><span class='keyword'>in</span><span> source) { </span> </li><li><span> </span><span class='keyword'>var</span><span> _property=(fProperty || Util.emptyFunction).call(source,property,destination); </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (_property!=</span><span class='keyword'>false</span><span>) { </span> </li><li><span> </span><span class='keyword'>var</span><span> _value=(fValue!=</span><span class='keyword'>null</span><span>? </span> </li><li class='alt'><span> fValue.call(source,source[property],property,destination): </span> </li><li><span> source[property]); </span> </li><li class='alt'><span> destination[</span><span class='keyword'>typeof</span><span> _property=='string'?_property:property]=_value; </span> </li><li><span> } </span> </li><li class='alt'><span> } </span> </li><li><span> </span><span class='keyword'>return</span><span> destination; </span> </li><li class='alt'><span> } </span> </li><li><span> }; </span> </li><li class='alt'><span> </span><span class='comment'>/* </span> </li><li><span><span class='comment'> * extends Array </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> (</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> Util.extend(Array.prototype,{ </span> </li><li><span> first:</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span> </span><span class='keyword'>this</span><span>[0]; </span> </li><li><span> }, </span> </li><li class='alt'><span> last:</span><span class='keyword'>function</span><span>() { </span> </li><li><span> </span><span class='keyword'>return</span><span> </span><span class='keyword'>this</span><span>[</span><span class='keyword'>this</span><span>.length-1]; </span> </li><li class='alt'><span> }, </span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 给出element在队列中所处的位置索引(第一个),如果不存在则返回-1。参数struct为true是进行严格比较 </span> </span> </li><li><span><span class='comment'> * @param {Object} element </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Boolean} struct[default=false] </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> indexOf:</span><span class='keyword'>function</span><span>(element,struct) { </span> </li><li><span> </span><span class='keyword'>var</span><span> size=</span><span class='keyword'>this</span><span>.length; </span> </li><li class='alt'><span> </span><span class='keyword'>for</span><span> (</span><span class='keyword'>var</span><span> i=0;i </span></li><li><span> </span><span class='keyword'>if</span><span> ((struct && </span><span class='keyword'>this</span><span>[i]===element) || </span> </li><li class='alt'><span> (!struct && </span><span class='keyword'>this</span><span>[i]==element)) { </span> </li><li><span> </span><span class='keyword'>break</span><span>; </span> </li><li class='alt'><span> } </span> </li><li><span> } </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span> (i==size?-1:i); </span> </li><li><span> }, </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 在索引index处插入元素element1,element2,.... </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Number} index </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> insertAt:</span><span class='keyword'>function</span><span>(index) { </span> </li><li><span> </span><span class='keyword'>var</span><span> args=[index,0]; </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> size=arguments.length; </span> </li><li><span> </span><span class='keyword'>for</span><span> (</span><span class='keyword'>var</span><span> i=1;i </span></li><li class='alt'><span> args.push(arguments[i]); </span> </li><li><span> } </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>.splice.apply(</span><span class='keyword'>this</span><span>,args); </span> </li><li><span> }, </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 删除索引index处的size个元素,并返回删除的元素数组 </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Number} index </span> </span> </li><li><span><span class='comment'> * @param {Number} size[opt=1] </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> deleteAt:</span><span class='keyword'>function</span><span>(index,size) { </span> </li><li class='alt'><span> result=</span><span class='keyword'>this</span><span>.splice(index,size || 1); </span> </li><li><span> </span><span class='keyword'>return</span><span> result; </span> </li><li class='alt'><span> }, </span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 清空队列 </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> clear:</span><span class='keyword'>function</span><span>() { </span> </li><li><span> </span><span class='keyword'>this</span><span>.length=0; </span> </li><li class='alt'><span> } </span> </li><li><span> }); </span> </li><li class='alt'><span> })(); </span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 事件队列,用于注册和注销事件。并触发对应事件 </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> jsLoader.EventQueue=(</span><span class='keyword'>function</span><span>() { </span> </li><li><span> Util.extend(ERRORS,{ </span> </li><li class='alt'><span> 'EventQueue.NO_SUCH_EVENT':ERROR_LEVEL.DEBUG+'no such a event', </span> </li><li><span> 'EventQueue.ALEADY_EXIST':ERROR_LEVEL.DEBUG+'the message is already exsited!' </span> </li><li class='alt'><span> }); </span> </li><li><span> </span><span class='keyword'>var</span><span> constructor=</span><span class='keyword'>function</span><span>(event) { </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>._queue={}; </span> </li><li><span> </span><span class='keyword'>this</span><span>.EVENT={}; </span> </li><li class='alt'><span> </span><span class='keyword'>for</span><span> (</span><span class='keyword'>var</span><span> e </span><span class='keyword'>in</span><span> event) { </span> </li><li><span> </span><span class='keyword'>this</span><span>.EVENT[event[e]]=</span><span class='keyword'>true</span><span>; </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>._queue[event[e]]=[]; </span> </li><li><span> } </span> </li><li class='alt'><span> }; </span> </li><li><span> Util.extend(constructor.prototype,{ </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 触发队列中,对应事件event的消息。参数dispose为ture时,将在触发完后删除该事件的所有消息。 </span> </span> </li><li class='alt'><span><span class='comment'> * @param {String} event </span> </span> </li><li><span><span class='comment'> * @param {Boolean} dispose[default=false] </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Array} args[default=[]] </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> dispatch:</span><span class='keyword'>function</span><span>(event,dispose,args) { </span> </li><li><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>this</span><span>.EVENT[event]) { </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> messages=</span><span class='keyword'>this</span><span>._queue[event]; </span> </li><li><span> </span><span class='keyword'>var</span><span> size=messages.length; </span> </li><li class='alt'><span> </span><span class='keyword'>try</span><span> { </span> </li><li><span> </span><span class='keyword'>for</span><span> (</span><span class='keyword'>var</span><span> i=0;i </span></li><li class='alt'><span> messages[i++].apply(messages[i++],messages[i++].concat(args || [])); </span> </li><li><span> } </span> </li><li class='alt'><span> } </span><span class='keyword'>catch</span><span>(e) { </span> </li><li><span> </span><span class='keyword'>throw</span><span> </span><span class='keyword'>new</span><span> Error(ERROR_LEVEL.DEBUG+e.message); </span> </li><li class='alt'><span> } </span><span class='keyword'>finally</span><span> { </span> </li><li><span> </span><span class='keyword'>if</span><span> (dispose) { </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>._queue[event].clear(); </span> </li><li><span> } </span> </li><li class='alt'><span> } </span> </li><li><span> } </span><span class='keyword'>else</span><span> { </span> </li><li class='alt'><span> </span><span class='keyword'>throw</span><span> </span><span class='keyword'>new</span><span> Error(ERRORS['EventQueue.NO_SUCH_EVENT']+':'+event); </span> </li><li><span> } </span> </li><li class='alt'><span> }, </span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 注册事件event的消息message。scope和args参数指定了消息调用时的作用域和参数。 </span> </span> </li><li><span><span class='comment'> * @param {String} event </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Function} message </span> </span> </li><li><span><span class='comment'> * @param {Object} scope[default=window] </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Array} args[default=[]] </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> on:</span><span class='keyword'>function</span><span>(event,message,scope,args) { </span> </li><li><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>this</span><span>.EVENT[event]) { </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>this</span><span>._queue[event].indexOf(message)==-1) { </span> </li><li><span> </span><span class='keyword'>this</span><span>._queue[event].push(message); </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>._queue[event].push(scope || window); </span> </li><li><span> </span><span class='keyword'>this</span><span>._queue[event].push(args || []); </span> </li><li class='alt'><span> } </span><span class='keyword'>else</span><span> { </span> </li><li><span> </span><span class='keyword'>throw</span><span> </span><span class='keyword'>new</span><span> Error(ERRORS['EventQueue.ALEADY_EXIST']+':'+event); </span> </li><li class='alt'><span> } </span> </li><li><span> } </span><span class='keyword'>else</span><span> { </span> </li><li class='alt'><span> </span><span class='keyword'>throw</span><span> </span><span class='keyword'>new</span><span> Error(ERRORS['EventQueue.NO_SUCH_EVENT']+':'+event); </span> </li><li><span> } </span> </li><li class='alt'><span> }, </span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 注销事件event的小心message </span> </span> </li><li><span><span class='comment'> * @param {String} event </span> </span> </li><li class='alt'><span><span class='comment'> * @param {Function} message </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> un:</span><span class='keyword'>function</span><span>(event,message) { </span> </li><li><span> </span><span class='keyword'>var</span><span> index; </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>this</span><span>.EVENT[event] && (index=</span><span class='keyword'>this</span><span>._queue[event].indexOf(message))!=-1) { </span> </li><li><span> </span><span class='keyword'>this</span><span>._queue[event].deleteAt(index,3); </span> </li><li class='alt'><span> } </span><span class='keyword'>else</span><span> { </span> </li><li><span> </span><span class='keyword'>throw</span><span> </span><span class='keyword'>new</span><span> Error(ERRORS['EventQueue.NO_SUCH_EVENT']+':'+event); </span> </li><li class='alt'><span> } </span> </li><li><span> } </span> </li><li class='alt'><span> }); </span> </li><li><span> </span><span class='keyword'>return</span><span> constructor; </span> </li><li class='alt'><span> })(); </span> </li><li><span> </span><span class='comment'>/* </span> </li><li class='alt'><span><span class='comment'> * a class of message queue, used to invoke messages orderly </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> jsLoader.MessageQueue=(</span><span class='keyword'>function</span><span>() { </span> </li><li><span> </span><span class='keyword'>var</span><span> constructor=</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>._queue=[]; </span> </li><li><span> </span><span class='keyword'>this</span><span>._listened=</span><span class='keyword'>null</span><span>; </span> </li><li class='alt'><span> } </span> </li><li><span> Util.extend(constructor.prototype,{ </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 队列监听器,识别队列中是否有可以产生的消息 </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> _listen:</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> _this=</span><span class='keyword'>this</span><span>; </span> </li><li><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>this</span><span>._listened) { </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>this</span><span>._queue.length==0) { </span> </li><li><span> setTimeout(</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> _this._listen() </span> </li><li><span> },500); </span> </li><li class='alt'><span> } </span><span class='keyword'>else</span><span> { </span> </li><li><span> </span><span class='keyword'>this</span><span>.inform(); </span> </li><li class='alt'><span> } </span> </li><li><span> } </span> </li><li class='alt'><span> }, </span> </li><li><span> </span><span class='comment'>/** </span> </li><li class='alt'><span><span class='comment'> * 启动监听器 </span> </span> </li><li><span><span class='comment'> */</span><span> </span></span> </li><li class='alt'><span> startListen:</span><span class='keyword'>function</span><span>() { </span> </li><li><span> </span><span class='keyword'>this</span><span>._listened=</span><span class='keyword'>true</span><span>; </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>._listen(); </span> </li><li><span> }, </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 停止监听器 </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> stopListen:</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> </span><span class='keyword'>this</span><span>._listened=</span><span class='keyword'>false</span><span>; </span> </li><li><span> }, </span> </li><li class='alt'><span> </span><span class='comment'>/** </span> </li><li><span><span class='comment'> * 通知消息队列,另起产生队列头部的一个消息 </span> </span> </li><li class='alt'><span><span class='comment'> */</span><span> </span></span> </li><li><span> inform:</span><span class='keyword'>function</span><span>() { </span> </li><li class='alt'><span> </span><span class='keyword'>if</span><span> (</span><span class='keyword'>this</span><span>._queue.length==0) { </span> </li><li><span> </span><span class='keyword'>this</span><span>._listen(); </span> </li><li class='alt'><span> </span><span class='keyword'>return</span><span>; </span> </li><li><span> } </span><span class='keyword'>else</span><span> { </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> message=</span><span class='keyword'>this</span><span>._queue.shift(); </span> </li><li><span> </span><span class='keyword'>var</span><span> scope=</span><span class='keyword'>this</span><span>._queue.shift(); </span> </li><li class='alt'><span> </span><span class='keyword'>var</span><span> args=</span><span class='keyword'>this</span><span>._queue.shift(); </span> </li><li><span> </span><span class='keyword'>try</span><span> { </span> </li><li><br/></li></ol></div></div><div class='quote_div'>呵呵,原来在12月份就有这么个东西发布了呢,看样子不错,不过代码没仔细看。对于bingo我也很长时间没空去进一步完善了,希望来年来完善一下。冒味问下,你是与狼共舞?</div>
1 楼
sun2grit
2008-01-12
能举几个简单的调用示例吗?
相关推荐
Tom Negrino是一位知名的技术作家,他在他的JavaScript教程中提出了一个Bingo游戏的实现,旨在帮助学习者实践和提升JavaScript编程技能。Bingo游戏通常涉及随机数字生成和匹配玩家卡片上的数字,这需要对数组操作、...
在本压缩包中,我们关注的是一个网络游戏的实现,具体来说是使用JavaScript语言构建的Bingo游戏。Bingo游戏是一种流行的游戏,玩家需要匹配随机抽取的数字与他们卡片上的数字,当一行、一列或特定模式完全匹配时,...
【Bingo游戏C#开发】是一款基于C#编程语言实现的经典小游戏,旨在为用户提供趣味性的娱乐体验。在本文中,我们将深入探讨C#语言在游戏开发中的应用,以及Bingo游戏的基本规则和实现原理。 首先,C#是一种由微软公司...
"前端开源库-bingo-builder"是一个专注于前端开发的开源工具,它被称为"宾果生成器",主要用于代码的合并与构建流程。在现代前端开发中,构建工具是必不可少的一部分,它们可以帮助开发者自动化处理代码,提升开发...
配置是Bingo框架中的一个重要环节,GlobalConfig、HooksConfig等配置类允许开发者根据项目需求进行个性化设置。同时,Bingo框架也提供了一套部署指南,指导开发者如何在不同的环境下部署和优化应用。 #### 库与规范...
本项目“bingo_fpga_verilog_数字时钟”是一个利用FPGA技术实现的数字时钟设计,它结合了EDA(Electronic Design Automation)工具和SOPC(System On a Programmable Chip)系统,展示了如何在实际应用中构建并验证...
在MATLAB环境中开发"Bingo"游戏,这是一款基于古典宾果规则的游戏,旨在提供一个有趣且具有挑战性的学习和实践编程的机会。MATLAB是一种强大的数值计算和数据分析工具,但同时也支持编写复杂的游戏逻辑和图形用户...
**菜单栏固定**与**非固定**的选项是Bingo Admin的另一个亮点。固定的菜单栏在页面滚动时始终保持可见,方便用户随时访问各个功能模块;而非固定的菜单栏则会在页面顶部,随着滚动逐渐隐藏,这样可以在有限的屏幕...
Bingo是一个强大的JavaScript库,专为.NET开发者设计,它提供了与.NET服务进行无缝通信的能力。Bingo的核心功能在于通过JSONP(JSON with Padding)或XMLHttpRequest(Ajax)等方式,使JavaScript能够跨域请求.NET...
【Bingo大屏幕1.3.7源码】是一个针对特定功能模块的软件开发资源包,主要用于构建或扩展具有互动游戏特性的大屏幕显示系统。这个版本号1.3.7表明了这是一个经过多次迭代和优化的成熟产品,通常包含了修复的bug、新增...
检疫宾果游戏 用Java编写的宾果游戏实现 去做: 添加游戏开始逻辑 添加游戏结束逻辑 添加排行榜 开发PWA版本 ...该项目欢迎捐助 :smiling_face: ,如果您有想要实施的建议/想法,请创建一个问题。
Bingo游戏是一种流行的卡片式游戏,游戏的规则是参赛者购买一张或多张Bingo票,每张票上有25个号码,包括5行5列,中间一格为空白区域,称作“Free Space”。玩家可以根据游戏的规则涂黑号码所在的格子,直到某个玩家...
在IT行业中,优化是至关重要的一个领域,尤其是在游戏开发中,比如本次提到的"Bingo Slot"游戏。这个项目是基于C#编程语言实现的,它使用了一种名为“离散差分进化”的优化算法来调整游戏的RTP(Return to Player)...
在这个特殊的案例中,开发者创建了一个专门针对OoT游戏的Bingo卡片生成器,让玩家在游戏中添加趣味性。 首先,我们来深入了解GitHub Pages。它是GitHub的一个特性,允许用户将特定的分支(通常是`gh-pages`分支)的...
bingo.sql
Android开发-完整的App标签表明,Android-Bingo项目不仅包含了一个基本的应用框架,还涵盖了从数据持久化、网络请求、UI设计到用户体验优化的完整开发流程。这意味着,对于想要学习Android开发的初学者或者寻求提高...
随机抽奖程序Bingo Game,可用于公司年会等活动幸运抽奖用。 用Excel表保存数据。数字游戏国外比较流行。
Bingo 抽奖器,适合各种年会!
"bingo:生成宾果网格的工具"是一个基于Python编程语言的实用程序,旨在帮助用户自定义并生成宾果游戏的网格。宾果游戏是一种广受欢迎的娱乐活动,尤其在社交聚会、教育课堂和社区活动中常见。这个工具的出现使得创建...
AJAX简单的框架。 使用方便,满足基本需求。