1// http://ejohn.org/blog/simple-javascript-inheritance/ 2 3(function() { 4 var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b__parent\b/ : /.*/; 5 6 function is_o(o) 7 { 8 return Object.prototype.toString.call(o) === "[object Object]"; 9 } 10 11 function clone(o) 12 { 13 if (o === null || typeof o !== 'object') 14 return o; 15 16 var c; 17 if (Array.isArray(o)) 18 c = o.slice(0); 19 else 20 { 21 try { 22 c = o.constructor(); 23 for (var key in o) 24 c[key] = clone(o[key]); 25 } 26 catch(e) { 27 c = o; 28 } 29 } 30 31 return c; 32 } 33 34 function merge(from, to) 35 { 36 var r = to ? clone(to) : {}; 37 var c = clone(from); 38 39 if(c === null || !is_o(c) || !is_o(r)) 40 return c; 41 42 for (var key in c) 43 r[key] = merge(c[key], r[key]); 44 45 return r; 46 } 47 48 // The base Class implementation (does nothing) 49 this.Class = function(){}; 50 51 // Create a new Class that inherits from this class 52 Class.extend = function(prop) 53 { 54 var __parent = this.prototype; 55 56 // Instantiate a base class (but only create the instance, don't run __construct) 57 initializing = true; 58 var prototype = new this(); 59 initializing = false; 60 61 // Copy the properties over onto the new prototype 62 for(var name in prop) 63 { 64 // Check if we're overwriting an existing function 65 if (typeof prop[name] == 'function' && typeof __parent[name] == 'function' && fnTest.test(prop[name])) 66 { 67 prototype[name] = (function(name, fn) { 68 return function() { 69 var tmp = this.__parent; 70 71 // Add a new .__parent() method that is the same method but on the super-class 72 this.__parent = __parent[name]; 73 74 // The method only need to be bound temporarily, so we remove it when we're done executing 75 var ret = fn.apply(this, arguments); 76 this.__parent = tmp; 77 78 return ret; 79 }; 80 })(name, prop[name]); 81 } 82 else if(is_o(prop[name])) 83 prototype[name] = merge(prop[name], __parent[name]); 84 else 85 prototype[name] = prop[name]; 86 } 87 88 // The dummy class constructor 89 function Class() 90 { 91 // All construction is actually done in __construct 92 if(!initializing) 93 { 94 // Make sure any non-function members are copies, to prevent shared objects amond instances 95 for(var name in prototype) 96 if(typeof prototype[name] != 'function') 97 this[name] = f3.extend(prototype[name]); 98 99 if(this.__construct) 100 this.__construct.apply(this, arguments); 101 } 102 } 103 104 // Populate our constructed prototype object 105 Class.prototype = prototype; 106 107 // Enforce the constructor to be what we expect 108 Class.prototype.constructor = Class; 109 110 // And make this class extendable 111 Class.extend = arguments.callee; 112 113 return Class; 114 }; 115 116 Class.merge = merge; 117})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.