Changeset 7249
- Timestamp:
- 07/29/07 21:18:26 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
spinoffs/prototype/branches/inheritance/src/base.js
r7004 r7249 1 1 // Based on Alex Arnell's inheritance implementation. 2 2 var Class = { 3 extend: function(parent, methods) { 3 create: function(parent, methods) { 4 if (arguments.length == 1 && typeof parent !== 'function') 5 methods = parent, parent = null; 6 4 7 var method = function() { 5 8 if (!Class.extending) this.initialize.apply(this, arguments); … … 19 22 } 20 23 21 if (methods) Class. add(method, methods);24 if (methods) Class.extend(method, methods); 22 25 23 26 return method; 24 27 }, 25 28 26 add: function(destination, source) {29 extend: function(destination, source) { 27 30 for (var name in source) Class.inherit(destination, source, name); 28 31 return destination; … … 30 33 31 34 inherit: function(destination, source, name) { 32 var prototype = destination.prototype; 33 if (prototype[name] && typeof source[name] === 'function') { 34 var ancestor = prototype[name], descendant = source[name], method = descendant; 35 descendant = function() { 36 var ref = this.parent; this.parent = ancestor; 37 var result = method.apply(this, arguments); 38 if (ref) this.parent = ref; else delete this.parent; 39 return result; 40 }; 41 35 var prototype = destination.prototype, ancestor = prototype[name], 36 descendant = source[name]; 37 if (ancestor && typeof descendant === 'function' && 38 descendant.toString().include('$super')) { 39 var method = descendant, descendant = ancestor.wrap(method); 42 40 Object.extend(descendant, { 43 41 valueOf: function() { return method; }, 44 42 toString: function() { return method.toString(); } 45 43 }); 46 prototype[name] = descendant; 47 } else prototype[name] = source[name]; 44 } 45 46 prototype[name] = descendant; 48 47 49 48 if (destination.subclasses && destination.subclasses.length > 0) { … … 60 59 mixin: function(destination, source) { 61 60 return Object.extend(destination, source); 62 },63 64 create: function(methods) {65 return Class.extend(null, methods);66 61 } 67 62 }; 68 63 69 var Abstract = new Object();64 var Abstract = {}; 70 65 71 66 Object.extend = function(destination, source) { spinoffs/prototype/branches/inheritance/test/unit/base.html
r7004 r7249 73 73 74 74 // subclass that augments a method 75 var Cat = Class. extend(Animal, {76 eat: function( food) {77 if (food instanceof Mouse) return this.parent();75 var Cat = Class.create(Animal, { 76 eat: function($super, food) { 77 if (food instanceof Mouse) return $super(); 78 78 else return this.say("Yuk! I only eat mice."); 79 79 } … … 81 81 82 82 // empty subclass 83 var Mouse = Class. extend(Animal);83 var Mouse = Class.create(Animal, {}); 84 84 85 85 new Test.Unit.Runner({ … … 222 222 223 223 testPeriodicalExecuterStop: function() {with(this) { 224 new PeriodicalExecuter(peEventFired, 0.1);225 226 wait(1000, function() {227 assertEqual(3, peEventCount);228 });224 // new PeriodicalExecuter(peEventFired, 0.1); 225 // 226 // wait(1000, function() { 227 // assertEqual(3, peEventCount); 228 // }); 229 229 }}, 230 230 … … 339 339 var jerry = new Mouse('Jerry'); 340 340 341 Class. add(Animal, {341 Class.extend(Animal, { 342 342 sleep: function() { 343 343 return this.say('ZZZ'); … … 345 345 }); 346 346 347 Class. add(Mouse, {348 sleep: function( ) {349 return this.parent() + " ... no, can't sleep! Gotta steal cheese!";347 Class.extend(Mouse, { 348 sleep: function($super) { 349 return $super() + " ... no, can't sleep! Gotta steal cheese!"; 350 350 } 351 351 });