jenkins-bot has submitted this change and it was merged.
Change subject: core: Remove dependency on Object.create
......................................................................
core: Remove dependency on Object.create
We claim not to use any unpolyfillable features of ES5, so let's not
depend on this method. We only depend on a subset of its features that can
be mostly polyfilled, so we were technically not lying, but it's just so
much simpler this way.
We now include a fallback implementing the minimal set of required features.
It's just a few lines, a small price to pay for the convenience.
It also has the benefit of not requiring users to assign
Object.create with a partial implementation, which can confuse
other libraries.
Bug: T109985
Change-Id: I63e23c7b56cc8003293345fb08ae02d7a9c4de54
---
M Gruntfile.js
M README.md
M src/Factory.js
M src/core.js
D tests/polyfill-object-create.js
M tests/unit/core.test.js
6 files changed, 56 insertions(+), 93 deletions(-)
Approvals:
Krinkle: Looks good to me, approved
Bartosz Dziewoński: Looks good to me, but someone else must approve
Jforrester: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/Gruntfile.js b/Gruntfile.js
index feefedc..710b9b9 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -95,7 +95,6 @@
files: [
'lib/json2.js',
'lib/es5-shim.js',
- 'tests/polyfill-object-create.js',
'dist/oojs.js',
'tests/testrunner.js',
'tests/unit/*.js'
diff --git a/README.md b/README.md
index 581e9de..df6b2f9 100644
--- a/README.md
+++ b/README.md
@@ -43,16 +43,17 @@
These are the methods used:
* Array.isArray
-* Object.create<br/><sup>(basic use only, inherit from an object and assign
simple properties)</sup>
* Object.keys
* JSON.stringify
-While there are no plans to ship a polyfill, we recommend the following and
use them ourselves in
-the unit tests to assert support for older browsers.
+Additionally, Object.create is used if available. As it is impossible to fully
polyfill, OOjs
+includes a fallback implementing the minimal set of required features.
+
+While there are no plans to ship a polyfill for remaining methods, we
recommend the following and
+use them ourselves in the unit tests to assert support for older browsers.
* [json2.js](https://github.com/douglascrockford/JSON-js) (only for IE6/IE7)
* [es5-shim.js](https://github.com/es-shims/es5-shim)
-* [polyfill-object-create.js](./test/polyfill-object-create.js)
jQuery
----------
diff --git a/src/Factory.js b/src/Factory.js
index d188f60..543497f 100644
--- a/src/Factory.js
+++ b/src/Factory.js
@@ -1,3 +1,5 @@
+/*global createObject */
+
/**
* @class OO.Factory
* @extends OO.Registry
@@ -98,7 +100,7 @@
// the constructor's prototype (which also makes it an "instanceof" the
constructor),
// then invoke the constructor with the object as context, and return
it (ignoring
// the constructor's return value).
- obj = Object.create( constructor.prototype );
+ obj = createObject( constructor.prototype );
constructor.apply( obj, args );
return obj;
};
diff --git a/src/core.js b/src/core.js
index 8eaa2d4..5e3168a 100644
--- a/src/core.js
+++ b/src/core.js
@@ -8,7 +8,21 @@
oo = {},
// Optimisation: Local reference to Object.prototype.hasOwnProperty
hasOwn = oo.hasOwnProperty,
- toString = oo.toString;
+ toString = oo.toString,
+ // Object.create() is impossible to fully polyfill, so don't require it
+ createObject = Object.create || ( function () {
+ // Reusable constructor function
+ function Empty() {}
+ return function ( prototype, properties ) {
+ var obj;
+ Empty.prototype = prototype;
+ obj = new Empty();
+ if ( properties && hasOwn.call( properties,
'constructor' ) ) {
+ obj.constructor = properties.constructor.value;
+ }
+ return obj;
+ };
+ } )();
/* Class Methods */
@@ -74,7 +88,7 @@
// allows people to comply with their style guide.
targetFn['super'] = targetFn.parent = originFn;
- targetFn.prototype = Object.create( originFn.prototype, {
+ targetFn.prototype = createObject( originFn.prototype, {
// Restore constructor property of targetFn
constructor: {
value: targetConstructor,
@@ -86,7 +100,7 @@
// Extend static properties - always initialize both sides
oo.initClass( originFn );
- targetFn.static = Object.create( originFn.static );
+ targetFn.static = createObject( originFn.static );
};
/**
@@ -228,7 +242,7 @@
oo.cloneObject = function ( origin ) {
var key, r;
- r = Object.create( origin.constructor.prototype );
+ r = createObject( origin.constructor.prototype );
for ( key in origin ) {
if ( hasOwn.call( origin, key ) ) {
diff --git a/tests/polyfill-object-create.js b/tests/polyfill-object-create.js
deleted file mode 100644
index 607faf6..0000000
--- a/tests/polyfill-object-create.js
+++ /dev/null
@@ -1,62 +0,0 @@
-/**
- * Simplified version of es5-sham#Object-create that also works around a bug
- * in the actual es5-sham: https://github.com/es-shims/es5-shim/issues/252
- *
- * Does not:
- * - Support empty inheritance via `Object.create(null)`.
- * - Support getter and setter accessors via `Object.create( .., properties )`.
- * - Support custom property descriptor (e.g. writable, configurtable,
enumerable).
- * - Leave behind an enumerable "__proto__" all over the place.
- *
- * @author Timo Tijhof, 2014
- */
-
-// ES5 15.2.3.5
-// http://es5.github.com/#x15.2.3.5
-if ( !Object.create ) {
- ( function () {
- var hasOwn = Object.hasOwnProperty,
- //
https://developer.mozilla.org/en-US/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
- //
http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
- isEnumBug = !{ valueOf: 0 }.propertyIsEnumerable(
'valueOf' );
-
- // Reusable constructor function for Object.create
- function Empty() {}
-
- function defineProperty( object, key, property ) {
- if ( hasOwn.call( property, 'value' ) ) {
- object[ key ] = property.value;
- } else {
- object[ key ] = property;
- }
- }
-
- Object.create = function create( prototype, properties ) {
- var object, key;
-
- if ( prototype !== Object( prototype ) ) {
- throw new TypeError( 'Called on non-object' );
- }
-
- Empty.prototype = prototype;
- object = new Empty();
-
- if ( properties !== undefined ) {
- if ( !isEnumBug ) {
- for ( key in properties ) {
- if ( hasOwn.call( properties,
key ) ) {
- defineProperty( object,
key, properties[ key ] );
- }
- }
- } else {
- Object.keys( properties ).forEach(
function ( key ) {
- defineProperty( object, key,
properties[ key ] );
- } );
- }
- }
-
- return object;
- };
-
- }() );
-}
diff --git a/tests/unit/core.test.js b/tests/unit/core.test.js
index 6e36da3..b8f0a60 100644
--- a/tests/unit/core.test.js
+++ b/tests/unit/core.test.js
@@ -373,15 +373,19 @@
'Function with properties'
);
- tmp = Object.create( { a: 1, b: false, foo: 'bar' } );
- tmp.b = true;
- tmp.bar = 'quux';
+ if ( !Object.create ) {
+ assert.strictEqual( true, true, '[skipped] Only own
properties' );
+ } else {
+ tmp = Object.create( { a: 1, b: false, foo: 'bar' } );
+ tmp.b = true;
+ tmp.bar = 'quux';
- assert.deepEqual(
- oo.getObjectValues( tmp ),
- [ true, 'quux' ],
- 'Only own properties'
- );
+ assert.deepEqual(
+ oo.getObjectValues( tmp ),
+ [ true, 'quux' ],
+ 'Only own properties'
+ );
+ }
assert.throws(
function () {
@@ -546,21 +550,26 @@
'Compare object to itself'
);
- x = Object.create( { foo: 1, map: function () { } } );
- x.foo = 2;
- x.bar = true;
+ if ( !Object.create ) {
+ assert.strictEqual( true, true, '[skipped] Ignore
inherited properties and methods of a' );
+ assert.strictEqual( true, true, '[skipped] Ignore
inherited properties and methods of b' );
+ } else {
+ x = Object.create( { foo: 1, map: function () { } } );
+ x.foo = 2;
+ x.bar = true;
- assert.strictEqual(
- oo.compare( x, { foo: 2, bar: true } ),
- true,
- 'Ignore inherited properties and methods of a'
- );
+ assert.strictEqual(
+ oo.compare( x, { foo: 2, bar: true } ),
+ true,
+ 'Ignore inherited properties and methods of a'
+ );
- assert.strictEqual(
- oo.compare( { foo: 2, bar: true }, x ),
- true,
- 'Ignore inherited properties and methods of b'
- );
+ assert.strictEqual(
+ oo.compare( { foo: 2, bar: true }, x ),
+ true,
+ 'Ignore inherited properties and methods of b'
+ );
+ }
assert.strictEqual(
oo.compare(
--
To view, visit https://gerrit.wikimedia.org/r/233222
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I63e23c7b56cc8003293345fb08ae02d7a9c4de54
Gerrit-PatchSet: 5
Gerrit-Project: oojs/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits