Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/112148
Change subject: core: Add a 'super' property to inheriting classes
......................................................................
core: Add a 'super' property to inheriting classes
* Fix typo in example where it said ".prop" (doesn't exist)
instead of ".static".
* Show usage example for the new super property.
* Add unit tests for super.
* Improve existing unit tests to test that instance properties
added by the constructor are being set as expected.
Change-Id: Ic301140d2e0ad99eddc83f9031e12a7641fd10ae
---
M src/core.js
M test/oo.core.test.js
2 files changed, 30 insertions(+), 15 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/oojs/core refs/changes/48/112148/1
diff --git a/src/core.js b/src/core.js
index 1e4d026..8e3d1da 100644
--- a/src/core.js
+++ b/src/core.js
@@ -50,23 +50,27 @@
* multiple constructors consider storing an instance of the other
constructor in a
* property instead, or perhaps use a mixin (see oo.mixinClass).
*
- * function Foo() {}
- * Foo.prototype.jump = function () {};
+ * function Thing() {}
+ * Thing.prototype.exists = function () {};
*
- * function FooBar() {}
- * oo.inheritClass( FooBar, Foo );
- * FooBar.prop.feet = 2;
- * FooBar.prototype.walk = function () {};
+ * function Person() {
+ * this.constructor.super.apply( this, arguments );
+ * }
+ * oo.inheritClass( Person, Thing );
+ * Person.static.defaultEyeCount = 2;
+ * Person.prototype.walk = function () {};
*
- * function FooBarQuux() {}
- * OO.inheritClass( FooBarQuux, FooBar );
- * FooBarQuux.prototype.jump = function () {};
+ * function Jumper() {
+ * this.constructor.super.apply( this, arguments );
+ * }
+ * OO.inheritClass( Jumper, Person );
+ * Jumper.prototype.jump = function () {};
*
- * FooBarQuux.prop.feet === 2;
- * var fb = new FooBar();
- * fb.jump();
- * fb.walk();
- * fb instanceof Foo && fb instanceof FooBar && fb instanceof FooBarQuux;
+ * Jumper.static.defaultExtremities === 2;
+ * var x = new Jumper();
+ * x.jump();
+ * x.walk();
+ * x instanceof Thing && x instanceof Person && x instanceof Jumper;
*
* @method
* @param {Function} targetFn
@@ -80,6 +84,7 @@
var targetConstructor = targetFn.prototype.constructor;
+ targetFn.super = originFn;
targetFn.prototype = Object.create( originFn.prototype, {
// Restore constructor property of targetFn
constructor: {
diff --git a/test/oo.core.test.js b/test/oo.core.test.js
index 3609bf9..f1f6cb9 100644
--- a/test/oo.core.test.js
+++ b/test/oo.core.test.js
@@ -86,7 +86,7 @@
} );
}
-QUnit.test( 'inheritClass', 19, function ( assert ) {
+QUnit.test( 'inheritClass', 26, function ( assert ) {
var foo, bar, key, enumKeys;
function Foo() {
@@ -107,6 +107,7 @@
foo = new Foo();
function Bar() {
+ this.constructor.super.call( this );
this.constructedBar = true;
}
oo.inheritClass( Bar, Foo );
@@ -171,12 +172,21 @@
'bar instance of Bar'
);
+ assert.equal( foo.constructor, Foo, 'original constructor is unchanged'
);
+ assert.equal( foo.constructedFoo, true, 'original constructor ran' );
+ assert.equal( foo.constructedBar, undefined, 'subclass did not modify
parent class' );
+
assert.equal( bar.constructor, Bar, 'constructor property is restored'
);
+ assert.equal( bar.constructor.super, Foo, 'super property points to
parent class' );
+ assert.equal( bar.constructedFoo, true, 'parent class ran through
this.constructor.super' );
+ assert.equal( bar.constructedBar, true, 'original constructor ran' );
assert.equal( bar.b, 'proto of Bar', 'own methods go first' );
assert.equal( bar.bFn(), 'proto of Bar', 'own properties go first' );
assert.equal( bar.c, 'proto of Foo', 'prototype properties are
inherited' );
assert.equal( bar.cFn(), 'proto of Foo', 'prototype methods are
inherited' );
+ assert.equal( bar.constructor.super, Foo, 'super property points to
parent class' );
+
enumKeys = [];
for ( key in bar ) {
enumKeys.push( key );
--
To view, visit https://gerrit.wikimedia.org/r/112148
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic301140d2e0ad99eddc83f9031e12a7641fd10ae
Gerrit-PatchSet: 1
Gerrit-Project: oojs/core
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits