Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/204048
Change subject: core: Support static properties on constructor object
......................................................................
core: Support static properties on constructor object
Copy static properties from the parent constructor.
This makes it possible for static methods to know in context of
which class they're called, as 'this' now refers to the subclass.
(Late static binding, effectively).
Bug: T89721
Change-Id: Ic0f38901a94922aa05e86b1f8efc6f678c8962ff
---
M src/core.js
1 file changed, 26 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/oojs/core refs/changes/48/204048/1
diff --git a/src/core.js b/src/core.js
index 3fcce43..aefd7a4 100644
--- a/src/core.js
+++ b/src/core.js
@@ -10,6 +10,19 @@
hasOwn = oo.hasOwnProperty,
toString = oo.toString;
+function copyStatic( target, origin ) {
+ for ( var key in origin ) {
+ if ( hasOwn.call( origin, key ) &&
+ // Reserved (JavaScript)
+ key !== 'prototype' && key !== 'constructor' &&
+ // Reserved (OOjs)
+ key !== 'super' && key !== 'parent' && key !== 'static'
+ ) {
+ target[key] = origin[key];
+ }
+ }
+}
+
/* Class Methods */
/**
@@ -84,9 +97,13 @@
}
} );
- // Extend static properties - always initialize both sides
+ // Set up inheritance for "static" container
+ // Initialize both sides so code can always add properties
oo.initClass( originFn );
targetFn.static = Object.create( originFn.static );
+
+ // Copy static constructor properties
+ copyStatic( targetFn, originFn );
};
/**
@@ -130,17 +147,21 @@
}
}
- // Copy static properties - always initialize both sides
+ // Copy "static" container properties
+ // Initialize both sides so code can always add properties
oo.initClass( targetFn );
- if ( originFn.static ) {
+ if ( !originFn.static ) {
+ oo.initClass( originFn );
+ } else {
for ( key in originFn.static ) {
if ( hasOwn.call( originFn.static, key ) ) {
targetFn.static[key] = originFn.static[key];
}
}
- } else {
- oo.initClass( originFn );
}
+
+ // Copy static constructor properties
+ copyStatic( targetFn, originFn );
};
/* Object Methods */
--
To view, visit https://gerrit.wikimedia.org/r/204048
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic0f38901a94922aa05e86b1f8efc6f678c8962ff
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