CCClass.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /* Managed JavaScript Inheritance
  23. * Based on John Resig's Simple JavaScript Inheritance http://ejohn.org/blog/simple-javascript-inheritance/
  24. * MIT Licensed.
  25. */
  26. /**
  27. * @namespace
  28. */
  29. var cc = cc || {};
  30. //
  31. var ClassManager = {
  32. id : (0|(Math.random()*998)),
  33. instanceId : (0|(Math.random()*998)),
  34. compileSuper : function(func, name, id){
  35. //make the func to a string
  36. var str = func.toString();
  37. //find parameters
  38. var pstart = str.indexOf('(');
  39. var pend = str.indexOf(')');
  40. var params = str.substring(pstart+1, pend);
  41. params = params.trim();
  42. //find function body
  43. var bstart = str.indexOf('{');
  44. var bend = str.lastIndexOf('}');
  45. var str = str.substring(bstart+1, bend);
  46. //now we have the content of the function, replace this._super
  47. //find this._super
  48. while(str.indexOf('this._super')!= -1)
  49. {
  50. var sp = str.indexOf('this._super');
  51. //find the first '(' from this._super)
  52. var bp = str.indexOf('(', sp);
  53. //find if we are passing params to super
  54. var bbp = str.indexOf(')', bp);
  55. var superParams = str.substring(bp+1, bbp);
  56. superParams = superParams.trim();
  57. var coma = superParams? ',':'';
  58. //replace this._super
  59. str = str.substring(0, sp)+ 'ClassManager['+id+'].'+name+'.call(this'+coma+str.substring(bp+1);
  60. }
  61. return Function(params, str);
  62. },
  63. getNewID : function(){
  64. return this.id++;
  65. },
  66. getNewInstanceId : function(){
  67. return this.instanceId++;
  68. }
  69. };
  70. ClassManager.compileSuper.ClassManager = ClassManager;
  71. (function () {
  72. var initializing = false, fnTest = /\b_super\b/;
  73. var releaseMode = (document['ccConfig'] && document['ccConfig']['CLASS_RELEASE_MODE']) ? document['ccConfig']['CLASS_RELEASE_MODE'] : null;
  74. if(releaseMode) {
  75. console.log("release Mode");
  76. }
  77. /**
  78. * The base Class implementation (does nothing)
  79. * @class
  80. */
  81. cc.Class = function () {
  82. };
  83. /**
  84. * Create a new Class that inherits from this Class
  85. * @param {object} prop
  86. * @return {function}
  87. */
  88. cc.Class.extend = function (prop) {
  89. var _super = this.prototype;
  90. // Instantiate a base Class (but only create the instance,
  91. // don't run the init constructor)
  92. var prototype = Object.create(_super);
  93. var classId = ClassManager.getNewID();
  94. ClassManager[classId] = _super;
  95. // Copy the properties over onto the new prototype. We make function
  96. // properties non-eumerable as this makes typeof === 'function' check
  97. // unneccessary in the for...in loop used 1) for generating Class()
  98. // 2) for cc.clone and perhaps more. It is also required to make
  99. // these function properties cacheable in Carakan.
  100. var desc = { writable: true, enumerable: false, configurable: true };
  101. for (var name in prop) {
  102. if(releaseMode && typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name])) {
  103. desc.value = ClassManager.compileSuper(prop[name], name, classId);
  104. Object.defineProperty(prototype, name, desc);
  105. } else if(typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name])){
  106. desc.value = (function (name, fn) {
  107. return function () {
  108. var tmp = this._super;
  109. // Add a new ._super() method that is the same method
  110. // but on the super-Class
  111. this._super = _super[name];
  112. // The method only need to be bound temporarily, so we
  113. // remove it when we're done executing
  114. var ret = fn.apply(this, arguments);
  115. this._super = tmp;
  116. return ret;
  117. };
  118. })(name, prop[name]);
  119. Object.defineProperty(prototype, name, desc);
  120. } else if(typeof prop[name] == "function") {
  121. desc.value = prop[name];
  122. Object.defineProperty(prototype, name, desc);
  123. } else{
  124. prototype[name] = prop[name];
  125. }
  126. }
  127. prototype.__instanceId = null;
  128. // The dummy Class constructor
  129. function Class() {
  130. this.__instanceId = ClassManager.getNewInstanceId();
  131. // All construction is actually done in the init method
  132. if (this.ctor)
  133. this.ctor.apply(this, arguments);
  134. }
  135. Class.id = classId;
  136. // desc = { writable: true, enumerable: false, configurable: true,
  137. // value: XXX }; Again, we make this non-enumerable.
  138. desc.value = classId;
  139. Object.defineProperty(prototype, '__pid', desc);
  140. // Populate our constructed prototype object
  141. Class.prototype = prototype;
  142. // Enforce the constructor to be what we expect
  143. desc.value = Class;
  144. Object.defineProperty(Class.prototype, 'constructor', desc);
  145. // And make this Class extendable
  146. Class.extend = cc.Class.extend;
  147. //add implementation method
  148. Class.implement = function (prop) {
  149. for (var name in prop) {
  150. prototype[name] = prop[name];
  151. }
  152. };
  153. return Class;
  154. };
  155. Function.prototype.bind = Function.prototype.bind || function (bind) {
  156. var self = this;
  157. var args = Array.prototype.slice.call(arguments, 1);
  158. return function () {
  159. return self.apply(bind || null, args.concat(Array.prototype.slice.call(arguments)));
  160. };
  161. };
  162. })();
  163. //
  164. // Another way to subclass: Using Google Closure.
  165. // The following code was copied + pasted from goog.base / goog.inherits
  166. //
  167. cc.inherits = function (childCtor, parentCtor) {
  168. /** @constructor */
  169. function tempCtor() {}
  170. tempCtor.prototype = parentCtor.prototype;
  171. childCtor.superClass_ = parentCtor.prototype;
  172. childCtor.prototype = new tempCtor();
  173. childCtor.prototype.constructor = childCtor;
  174. // Copy "static" method, but doesn't generate subclasses.
  175. // for( var i in parentCtor ) {
  176. // childCtor[ i ] = parentCtor[ i ];
  177. // }
  178. };
  179. cc.base = function(me, opt_methodName, var_args) {
  180. var caller = arguments.callee.caller;
  181. if (caller.superClass_) {
  182. // This is a constructor. Call the superclass constructor.
  183. ret = caller.superClass_.constructor.apply( me, Array.prototype.slice.call(arguments, 1));
  184. return ret;
  185. }
  186. var args = Array.prototype.slice.call(arguments, 2);
  187. var foundCaller = false;
  188. for (var ctor = me.constructor; ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
  189. if (ctor.prototype[opt_methodName] === caller) {
  190. foundCaller = true;
  191. } else if (foundCaller) {
  192. return ctor.prototype[opt_methodName].apply(me, args);
  193. }
  194. }
  195. // If we did not find the caller in the prototype chain,
  196. // then one of two things happened:
  197. // 1) The caller is an instance method.
  198. // 2) This method was not called by the right caller.
  199. if (me[opt_methodName] === caller) {
  200. return me.constructor.prototype[opt_methodName].apply(me, args);
  201. } else {
  202. throw Error(
  203. 'cc.base called from a method of one name ' +
  204. 'to a method of a different name');
  205. }
  206. };
  207. cc.concatObjectProperties = function(dstObject, srcObject){
  208. if(!dstObject)
  209. dstObject = {};
  210. for(var selKey in srcObject){
  211. dstObject[selKey] = srcObject[selKey];
  212. }
  213. return dstObject;
  214. };