From 0c963d1d00b5ed392c5648a2a56c9a3db56e7877 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 10 Nov 2010 22:54:24 -0500 Subject: [PATCH] Added access to parent prototype via parent property --- lib/class.js | 3 +++ test/test-class-parent.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/class.js b/lib/class.js index ed5fa15..d121025 100644 --- a/lib/class.js +++ b/lib/class.js @@ -111,6 +111,9 @@ var extend = function() // copy the given properties into the new prototype prop_copy( props, prototype ); + // reference to the parent prototype (for more experienced users) + prototype.parent = base.prototype; + // set up the new class attach_extend( new_class ); new_class.prototype = prototype; diff --git a/test/test-class-parent.js b/test/test-class-parent.js index 5e52d57..4b1a246 100644 --- a/test/test-class-parent.js +++ b/test/test-class-parent.js @@ -59,6 +59,11 @@ var SubFoo = Foo.extend( { return this.__super( arg ); }, + + callParentAlt: function() + { + return this.parent.myMethod2.apply( this, arguments ); + }, }); var foo = new Foo(), @@ -100,3 +105,16 @@ assert.equal( "Arguments should be passed to super method via _super argument list" ); +assert.deepEqual( + SubFoo.prototype.parent, + Foo.prototype, + "Parent property should represent parent prototype" +); + +sub_foo.callParentAlt( arg = 'moo' ); +assert.equal( + sub_foo.method2Arg, + arg, + "The parent property may also be used to invoke parent methods" +); +