summaryrefslogtreecommitdiff
path: root/resources/src/polyfill-object-create.js
blob: 607faf649f1e462f32cd9d6912a3c078651c1b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
 * 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;
		};

	}() );
}