Core.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Core.js
  2. // For fundamental functions.
  3. // FZ, Copyright (c) 2010 Zlongames
  4. (function()
  5. {
  6. FZ = {
  7. // class function
  8. newClass:function( classMemberObj, baseClass )
  9. {
  10. if (classMemberObj === undefined) classMemberObj ={};
  11. var PrototypeObj= function() {};
  12. if (typeof baseClass === "function" && baseClass.hasOwnProperty("prototype"))
  13. PrototypeObj.prototype = baseClass.prototype;
  14. else if (typeof baseClass === "object")
  15. PrototypeObj.prototype = baseClass;
  16. var ClassConstructor = function(){};
  17. if (classMemberObj.hasOwnProperty("init") || "init" in PrototypeObj.prototype){
  18. ClassConstructor = function(){this.init.apply(this,arguments);};
  19. }
  20. ClassConstructor.prototype = new PrototypeObj();
  21. ClassConstructor.prototype.constructor = ClassConstructor;
  22. for (var i in classMemberObj) {
  23. ClassConstructor.prototype[i] = classMemberObj[i];
  24. }
  25. return ClassConstructor;
  26. },
  27. $: function (id) { return document.getElementById(id);},
  28. // tools fuction
  29. Tools:{
  30. // return an array to include the argument
  31. splat: function(obj){
  32. if (!obj) return [];
  33. if ( obj instanceof Array || (typeof obj === "object" && typeof obj.length === 'number' && obj.callee)/*Arguments*/)
  34. return obj;
  35. else return [obj];
  36. },
  37. bind: function(Obj, Func, Args)
  38. {
  39. return function(){return Func.apply(Obj, FZ.Tools.splat(Args)); };
  40. },
  41. bindWithEvent: function(Obj, Func, Args)
  42. {
  43. return function(event){
  44. var _args = [event];
  45. Array.prototype.push.apply(_args, FZ.Tools.splat(Args));
  46. return Func.apply(Obj, _args);
  47. };
  48. },
  49. trim: function (str)
  50. {
  51. return str.replace(/^\s+|\s+$/g, '');
  52. },
  53. extend: function (ObjA, ObjB)
  54. {
  55. for (var i in ObjB) ObjA[i] = ObjB[i];
  56. }
  57. },
  58. EngineOptions:
  59. {
  60. DebugLevel: "error" // 'none': no Debug info, 'error': error message only , 'info': info and error message
  61. },
  62. _trace: function(info)
  63. {
  64. if (FZ.EngineOptions.DebugLevel === "info" )
  65. console.log( info );
  66. },
  67. _assert: function(condition,info)
  68. {
  69. if ((FZ.EngineOptions.DebugLevel === "error"||FZ.EngineOptions.DebugLevel === "info") && (!condition))
  70. alert(info);
  71. }
  72. };
  73. })();