Merge "mediawiki.widgets: Remove use of bind() for lexical 'this' binding"
[lhc/web/wiklou.git] / resources / src / polyfill-object-create.js
1 /**
2 * Simplified version of es5-sham#Object-create that also works around a bug
3 * in the actual es5-sham: https://github.com/es-shims/es5-shim/issues/252
4 *
5 * Does not:
6 * - Support empty inheritance via `Object.create(null)`.
7 * - Support getter and setter accessors via `Object.create( .., properties )`.
8 * - Support custom property descriptor (e.g. writable, configurtable, enumerable).
9 * - Leave behind an enumerable "__proto__" all over the place.
10 *
11 * @author Timo Tijhof, 2014
12 */
13
14 // ES5 15.2.3.5
15 // http://es5.github.com/#x15.2.3.5
16 if ( !Object.create ) {
17 ( function () {
18 var hasOwn = Object.hasOwnProperty,
19 // https://developer.mozilla.org/en-US/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
20 // http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
21 isEnumBug = !{ valueOf: 0 }.propertyIsEnumerable( 'valueOf' );
22
23 // Reusable constructor function for Object.create
24 function Empty() {}
25
26 function defineProperty( object, key, property ) {
27 if ( hasOwn.call( property, 'value' ) ) {
28 object[ key ] = property.value;
29 } else {
30 object[ key ] = property;
31 }
32 }
33
34 Object.create = function create( prototype, properties ) {
35 var object, key;
36
37 if ( prototype !== Object( prototype ) ) {
38 throw new TypeError( 'Called on non-object' );
39 }
40
41 Empty.prototype = prototype;
42 object = new Empty();
43
44 if ( properties !== undefined ) {
45 if ( !isEnumBug ) {
46 for ( key in properties ) {
47 if ( hasOwn.call( properties, key ) ) {
48 defineProperty( object, key, properties[ key ] );
49 }
50 }
51 } else {
52 Object.keys( properties ).forEach( function ( key ) {
53 defineProperty( object, key, properties[ key ] );
54 } );
55 }
56 }
57
58 return object;
59 };
60
61 }() );
62 }