Merge "Remove 2 false values returned in execute()"
[lhc/web/wiklou.git] / resources / oojs / oojs.js
1 /*!
2 * OOjs v1.0.7-pre (22e610a5e9)
3 * https://www.mediawiki.org/wiki/OOjs
4 *
5 * Copyright 2011-2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
8 *
9 * Date: Fri Mar 07 2014 17:36:23 GMT-0800 (PST)
10 */
11 ( function ( global ) {
12
13 'use strict';
14 var
15 /**
16 * Namespace for all classes, static methods and static properties.
17 * @class OO
18 * @singleton
19 */
20 oo = {},
21 hasOwn = oo.hasOwnProperty,
22 toString = oo.toString;
23
24 /* Class Methods */
25
26 /**
27 * Assert whether a value is a plain object or not.
28 *
29 * @param {Mixed} obj
30 * @return {boolean}
31 */
32 oo.isPlainObject = function ( obj ) {
33 // Any object or value whose internal [[Class]] property is not "[object Object]"
34 if ( toString.call( obj ) !== '[object Object]' ) {
35 return false;
36 }
37
38 // The try/catch suppresses exceptions thrown when attempting to access
39 // the "constructor" property of certain host objects suich as window.location
40 // in Firefox < 20 (https://bugzilla.mozilla.org/814622)
41 try {
42 if ( obj.constructor &&
43 !hasOwn.call( obj.constructor.prototype, 'isPrototypeOf' ) ) {
44 return false;
45 }
46 } catch ( e ) {
47 return false;
48 }
49
50 return true;
51 };
52
53 /**
54 * Utility for common usage of Object#create for inheriting from one
55 * prototype to another.
56 *
57 * Beware: This redefines the prototype, call before setting your prototypes.
58 * Beware: This redefines the prototype, can only be called once on a function.
59 * If called multiple times on the same function, the previous prototype is lost.
60 * This is how prototypal inheritance works, it can only be one straight chain
61 * (just like classical inheritance in PHP for example). If you need to work with
62 * multiple constructors consider storing an instance of the other constructor in a
63 * property instead, or perhaps use a mixin (see OO.mixinClass).
64 *
65 * function Thing() {}
66 * Thing.prototype.exists = function () {};
67 *
68 * function Person() {
69 * Person.super.apply( this, arguments );
70 * }
71 * OO.inheritClass( Person, Thing );
72 * Person.static.defaultEyeCount = 2;
73 * Person.prototype.walk = function () {};
74 *
75 * function Jumper() {
76 * Jumper.super.apply( this, arguments );
77 * }
78 * OO.inheritClass( Jumper, Person );
79 * Jumper.prototype.jump = function () {};
80 *
81 * Jumper.static.defaultEyeCount === 2;
82 * var x = new Jumper();
83 * x.jump();
84 * x.walk();
85 * x instanceof Thing && x instanceof Person && x instanceof Jumper;
86 *
87 * @param {Function} targetFn
88 * @param {Function} originFn
89 * @throws {Error} If target already inherits from origin
90 */
91 oo.inheritClass = function ( targetFn, originFn ) {
92 if ( targetFn.prototype instanceof originFn ) {
93 throw new Error( 'Target already inherits from origin' );
94 }
95
96 var targetConstructor = targetFn.prototype.constructor;
97
98 targetFn.super = originFn;
99 targetFn.prototype = Object.create( originFn.prototype, {
100 // Restore constructor property of targetFn
101 constructor: {
102 value: targetConstructor,
103 enumerable: false,
104 writable: true,
105 configurable: true
106 }
107 } );
108
109 // Extend static properties - always initialize both sides
110 originFn.static = originFn.static || {};
111 targetFn.static = Object.create( originFn.static );
112 };
113
114 /**
115 * Utility to copy over *own* prototype properties of a mixin.
116 * The 'constructor' (whether implicit or explicit) is not copied over.
117 *
118 * This does not create inheritance to the origin. If inheritance is needed
119 * use oo.inheritClass instead.
120 *
121 * Beware: This can redefine a prototype property, call before setting your prototypes.
122 * Beware: Don't call before oo.inheritClass.
123 *
124 * function Foo() {}
125 * function Context() {}
126 *
127 * // Avoid repeating this code
128 * function ContextLazyLoad() {}
129 * ContextLazyLoad.prototype.getContext = function () {
130 * if ( !this.context ) {
131 * this.context = new Context();
132 * }
133 * return this.context;
134 * };
135 *
136 * function FooBar() {}
137 * OO.inheritClass( FooBar, Foo );
138 * OO.mixinClass( FooBar, ContextLazyLoad );
139 *
140 * @param {Function} targetFn
141 * @param {Function} originFn
142 */
143 oo.mixinClass = function ( targetFn, originFn ) {
144 var key;
145
146 // Copy prototype properties
147 for ( key in originFn.prototype ) {
148 if ( key !== 'constructor' && hasOwn.call( originFn.prototype, key ) ) {
149 targetFn.prototype[key] = originFn.prototype[key];
150 }
151 }
152
153 // Copy static properties - always initialize both sides
154 targetFn.static = targetFn.static || {};
155 if ( originFn.static ) {
156 for ( key in originFn.static ) {
157 if ( hasOwn.call( originFn.static, key ) ) {
158 targetFn.static[key] = originFn.static[key];
159 }
160 }
161 } else {
162 originFn.static = {};
163 }
164 };
165
166 /* Object Methods */
167
168 /**
169 * Create a new object that is an instance of the same
170 * constructor as the input, inherits from the same object
171 * and contains the same own properties.
172 *
173 * This makes a shallow non-recursive copy of own properties.
174 * To create a recursive copy of plain objects, use #copy.
175 *
176 * var foo = new Person( mom, dad );
177 * foo.setAge( 21 );
178 * var foo2 = OO.cloneObject( foo );
179 * foo.setAge( 22 );
180 *
181 * // Then
182 * foo2 !== foo; // true
183 * foo2 instanceof Person; // true
184 * foo2.getAge(); // 21
185 * foo.getAge(); // 22
186 *
187 * @param {Object} origin
188 * @return {Object} Clone of origin
189 */
190 oo.cloneObject = function ( origin ) {
191 var key, r;
192
193 r = Object.create( origin.constructor.prototype );
194
195 for ( key in origin ) {
196 if ( hasOwn.call( origin, key ) ) {
197 r[key] = origin[key];
198 }
199 }
200
201 return r;
202 };
203
204 /**
205 * Get an array of all property values in an object.
206 *
207 * @param {Object} Object to get values from
208 * @return {Array} List of object values
209 */
210 oo.getObjectValues = function ( obj ) {
211 var key, values;
212
213 if ( obj !== Object( obj ) ) {
214 throw new TypeError( 'Called on non-object' );
215 }
216
217 values = [];
218 for ( key in obj ) {
219 if ( hasOwn.call( obj, key ) ) {
220 values[values.length] = obj[key];
221 }
222 }
223
224 return values;
225 };
226
227 /**
228 * Recursively compares properties between two objects.
229 *
230 * A false result may be caused by property inequality or by properties in one object missing from
231 * the other. An asymmetrical test may also be performed, which checks only that properties in the
232 * first object are present in the second object, but not the inverse.
233 *
234 * @param {Object} a First object to compare
235 * @param {Object} b Second object to compare
236 * @param {boolean} [asymmetrical] Whether to check only that b contains values from a
237 * @return {boolean} If the objects contain the same values as each other
238 */
239 oo.compare = function ( a, b, asymmetrical ) {
240 var aValue, bValue, aType, bType, k;
241
242 if ( a === b ) {
243 return true;
244 }
245
246 for ( k in a ) {
247 aValue = a[k];
248 bValue = b[k];
249 aType = typeof aValue;
250 bType = typeof bValue;
251 if ( aType !== bType ||
252 ( ( aType === 'string' || aType === 'number' ) && aValue !== bValue ) ||
253 ( aValue === Object( aValue ) && !oo.compare( aValue, bValue, asymmetrical ) ) ) {
254 return false;
255 }
256 }
257 // If the check is not asymmetrical, recursing with the arguments swapped will verify our result
258 return asymmetrical ? true : oo.compare( b, a, true );
259 };
260
261 /**
262 * Create a plain deep copy of any kind of object.
263 *
264 * Copies are deep, and will either be an object or an array depending on `source`.
265 *
266 * @param {Object} source Object to copy
267 * @param {Function} [callback] Applied to leaf values before they added to the clone
268 * @return {Object} Copy of source object
269 */
270 oo.copy = function ( source, callback ) {
271 var key, sourceValue, sourceType, destination;
272
273 if ( typeof source.clone === 'function' ) {
274 return source.clone();
275 }
276
277 destination = Array.isArray( source ) ? new Array( source.length ) : {};
278
279 for ( key in source ) {
280 sourceValue = source[key];
281 sourceType = typeof sourceValue;
282 if ( Array.isArray( sourceValue ) ) {
283 // Array
284 destination[key] = oo.copy( sourceValue, callback );
285 } else if ( sourceValue && typeof sourceValue.clone === 'function' ) {
286 // Duck type object with custom clone method
287 destination[key] = callback ?
288 callback( sourceValue.clone() ) : sourceValue.clone();
289 } else if ( sourceValue && typeof sourceValue.cloneNode === 'function' ) {
290 // DOM Node
291 destination[key] = callback ?
292 callback( sourceValue.cloneNode( true ) ) : sourceValue.cloneNode( true );
293 } else if ( oo.isPlainObject( sourceValue ) ) {
294 // Plain objects
295 destination[key] = oo.copy( sourceValue, callback );
296 } else {
297 // Non-plain objects (incl. functions) and primitive values
298 destination[key] = callback ? callback( sourceValue ) : sourceValue;
299 }
300 }
301
302 return destination;
303 };
304
305 /**
306 * Generate a hash of an object based on its name and data.
307 *
308 * Performance optimization: <http://jsperf.com/ve-gethash-201208#/toJson_fnReplacerIfAoForElse>
309 *
310 * To avoid two objects with the same values generating different hashes, we utilize the replacer
311 * argument of JSON.stringify and sort the object by key as it's being serialized. This may or may
312 * not be the fastest way to do this; we should investigate this further.
313 *
314 * Objects and arrays are hashed recursively. When hashing an object that has a .getHash()
315 * function, we call that function and use its return value rather than hashing the object
316 * ourselves. This allows classes to define custom hashing.
317 *
318 * @param {Object} val Object to generate hash for
319 * @return {string} Hash of object
320 */
321 oo.getHash = function ( val ) {
322 return JSON.stringify( val, oo.getHash.keySortReplacer );
323 };
324
325 /**
326 * Helper function for OO.getHash which sorts objects by key.
327 *
328 * This is a callback passed into JSON.stringify.
329 *
330 * @method getHash_keySortReplacer
331 * @param {string} key Property name of value being replaced
332 * @param {Mixed} val Property value to replace
333 * @return {Mixed} Replacement value
334 */
335 oo.getHash.keySortReplacer = function ( key, val ) {
336 var normalized, keys, i, len;
337 if ( val && typeof val.getHashObject === 'function' ) {
338 // This object has its own custom hash function, use it
339 val = val.getHashObject();
340 }
341 if ( !Array.isArray( val ) && Object( val ) === val ) {
342 // Only normalize objects when the key-order is ambiguous
343 // (e.g. any object not an array).
344 normalized = {};
345 keys = Object.keys( val ).sort();
346 i = 0;
347 len = keys.length;
348 for ( ; i < len; i += 1 ) {
349 normalized[keys[i]] = val[keys[i]];
350 }
351 return normalized;
352
353 // Primitive values and arrays get stable hashes
354 // by default. Lets those be stringified as-is.
355 } else {
356 return val;
357 }
358 };
359
360 /**
361 * Compute the union (duplicate-free merge) of a set of arrays.
362 *
363 * Arrays values must be convertable to object keys (strings).
364 *
365 * By building an object (with the values for keys) in parallel with
366 * the array, a new item's existence in the union can be computed faster.
367 *
368 * @param {Array...} arrays Arrays to union
369 * @return {Array} Union of the arrays
370 */
371 oo.simpleArrayUnion = function () {
372 var i, ilen, arr, j, jlen,
373 obj = {},
374 result = [];
375
376 for ( i = 0, ilen = arguments.length; i < ilen; i++ ) {
377 arr = arguments[i];
378 for ( j = 0, jlen = arr.length; j < jlen; j++ ) {
379 if ( !obj[ arr[j] ] ) {
380 obj[ arr[j] ] = true;
381 result.push( arr[j] );
382 }
383 }
384 }
385
386 return result;
387 };
388
389 /**
390 * Combine arrays (intersection or difference).
391 *
392 * An intersection checks the item exists in 'b' while difference checks it doesn't.
393 *
394 * Arrays values must be convertable to object keys (strings).
395 *
396 * By building an object (with the values for keys) of 'b' we can
397 * compute the result faster.
398 *
399 * @private
400 * @param {Array} a First array
401 * @param {Array} b Second array
402 * @param {boolean} includeB Whether to items in 'b'
403 * @return {Array} Combination (intersection or difference) of arrays
404 */
405 function simpleArrayCombine( a, b, includeB ) {
406 var i, ilen, isInB,
407 bObj = {},
408 result = [];
409
410 for ( i = 0, ilen = b.length; i < ilen; i++ ) {
411 bObj[ b[i] ] = true;
412 }
413
414 for ( i = 0, ilen = a.length; i < ilen; i++ ) {
415 isInB = !!bObj[ a[i] ];
416 if ( isInB === includeB ) {
417 result.push( a[i] );
418 }
419 }
420
421 return result;
422 }
423
424 /**
425 * Compute the intersection of two arrays (items in both arrays).
426 *
427 * Arrays values must be convertable to object keys (strings).
428 *
429 * @param {Array} a First array
430 * @param {Array} b Second array
431 * @return {Array} Intersection of arrays
432 */
433 oo.simpleArrayIntersection = function ( a, b ) {
434 return simpleArrayCombine( a, b, true );
435 };
436
437 /**
438 * Compute the difference of two arrays (items in 'a' but not 'b').
439 *
440 * Arrays values must be convertable to object keys (strings).
441 *
442 * @param {Array} a First array
443 * @param {Array} b Second array
444 * @return {Array} Intersection of arrays
445 */
446 oo.simpleArrayDifference = function ( a, b ) {
447 return simpleArrayCombine( a, b, false );
448 };
449 /**
450 * @class OO.EventEmitter
451 *
452 * @constructor
453 */
454 oo.EventEmitter = function OoEventEmitter() {
455 // Properties
456
457 /**
458 * Storage of bound event handlers by event name.
459 *
460 * @property
461 */
462 this.bindings = {};
463 };
464
465 /* Methods */
466
467 /**
468 * Add a listener to events of a specific event.
469 *
470 * If the callback/context are already bound to the event, they will not be bound again.
471 *
472 * @param {string} event Type of event to listen to
473 * @param {Function} callback Function to call when event occurs
474 * @param {Array} [args] Arguments to pass to listener, will be prepended to emitted arguments
475 * @param {Object} [context=null] Object to use as context for callback function or call method on
476 * @throws {Error} Listener argument is not a function or method name
477 * @chainable
478 */
479 oo.EventEmitter.prototype.on = function ( event, callback, args, context ) {
480 var i, bindings, binding;
481
482 // Validate callback
483 if ( typeof callback !== 'function' ) {
484 throw new Error( 'Invalid callback. Function or method name expected.' );
485 }
486 // Fallback to null context
487 if ( arguments.length < 4 ) {
488 context = null;
489 }
490 if ( this.bindings.hasOwnProperty( event ) ) {
491 // Check for duplicate callback and context for this event
492 bindings = this.bindings[event];
493 i = bindings.length;
494 while ( i-- ) {
495 binding = bindings[i];
496 if ( bindings.callback === callback && bindings.context === context ) {
497 return this;
498 }
499 }
500 } else {
501 // Auto-initialize bindings list
502 bindings = this.bindings[event] = [];
503 }
504 // Add binding
505 bindings.push( {
506 callback: callback,
507 args: args,
508 context: context
509 } );
510 return this;
511 };
512
513 /**
514 * Adds a one-time listener to a specific event.
515 *
516 * @param {string} event Type of event to listen to
517 * @param {Function} listener Listener to call when event occurs
518 * @chainable
519 */
520 oo.EventEmitter.prototype.once = function ( event, listener ) {
521 var eventEmitter = this;
522 return this.on( event, function listenerWrapper() {
523 eventEmitter.off( event, listenerWrapper );
524 listener.apply( eventEmitter, Array.prototype.slice.call( arguments, 0 ) );
525 } );
526 };
527
528 /**
529 * Remove a specific listener from a specific event.
530 *
531 * @param {string} event Type of event to remove listener from
532 * @param {Function} [callback] Listener to remove, omit to remove all
533 * @param {Object} [context=null] Object used context for callback function or method
534 * @chainable
535 * @throws {Error} Listener argument is not a function
536 */
537 oo.EventEmitter.prototype.off = function ( event, callback, context ) {
538 var i, bindings;
539
540 if ( arguments.length === 1 ) {
541 // Remove all bindings for event
542 if ( event in this.bindings ) {
543 delete this.bindings[event];
544 }
545 } else {
546 if ( typeof callback !== 'function' ) {
547 throw new Error( 'Invalid callback. Function expected.' );
548 }
549 if ( !( event in this.bindings ) || !this.bindings[event].length ) {
550 // No matching bindings
551 return this;
552 }
553 // Fallback to null context
554 if ( arguments.length < 3 ) {
555 context = null;
556 }
557 // Remove matching handlers
558 bindings = this.bindings[event];
559 i = bindings.length;
560 while ( i-- ) {
561 if ( bindings[i].callback === callback && bindings[i].context === context ) {
562 bindings.splice( i, 1 );
563 }
564 }
565 // Cleanup if now empty
566 if ( bindings.length === 0 ) {
567 delete this.bindings[event];
568 }
569 }
570 return this;
571 };
572
573 /**
574 * Emit an event.
575 *
576 * TODO: Should this be chainable? What is the usefulness of the boolean
577 * return value here?
578 *
579 * @param {string} event Type of event
580 * @param {Mixed} args First in a list of variadic arguments passed to event handler (optional)
581 * @return {boolean} If event was handled by at least one listener
582 */
583 oo.EventEmitter.prototype.emit = function ( event ) {
584 var i, len, binding, bindings, args;
585
586 if ( event in this.bindings ) {
587 // Slicing ensures that we don't get tripped up by event handlers that add/remove bindings
588 bindings = this.bindings[event].slice();
589 args = Array.prototype.slice.call( arguments, 1 );
590 for ( i = 0, len = bindings.length; i < len; i++ ) {
591 binding = bindings[i];
592 binding.callback.apply(
593 binding.context,
594 binding.args ? binding.args.concat( args ) : args
595 );
596 }
597 return true;
598 }
599 return false;
600 };
601
602 /**
603 * Connect event handlers to an object.
604 *
605 * @param {Object} context Object to call methods on when events occur
606 * @param {Object.<string,string>|Object.<string,Function>|Object.<string,Array>} methods List of
607 * event bindings keyed by event name containing either method names, functions or arrays containing
608 * method name or function followed by a list of arguments to be passed to callback before emitted
609 * arguments
610 * @chainable
611 */
612 oo.EventEmitter.prototype.connect = function ( context, methods ) {
613 var method, callback, args, event;
614
615 for ( event in methods ) {
616 method = methods[event];
617 // Allow providing additional args
618 if ( Array.isArray( method ) ) {
619 args = method.slice( 1 );
620 method = method[0];
621 } else {
622 args = [];
623 }
624 // Allow callback to be a method name
625 if ( typeof method === 'string' ) {
626 // Validate method
627 if ( !context[method] || typeof context[method] !== 'function' ) {
628 throw new Error( 'Method not found: ' + method );
629 }
630 // Resolve to function
631 callback = context[method];
632 } else {
633 callback = method;
634 }
635 // Add binding
636 this.on.apply( this, [ event, callback, args, context ] );
637 }
638 return this;
639 };
640
641 /**
642 * Disconnect event handlers from an object.
643 *
644 * @param {Object} context Object to disconnect methods from
645 * @param {Object.<string,string>|Object.<string,Function>|Object.<string,Array>} [methods] List of
646 * event bindings keyed by event name containing either method names or functions
647 * @chainable
648 */
649 oo.EventEmitter.prototype.disconnect = function ( context, methods ) {
650 var i, method, callback, event, bindings;
651
652 if ( methods ) {
653 // Remove specific connections to the context
654 for ( event in methods ) {
655 method = methods[event];
656 if ( typeof method === 'string' ) {
657 // Validate method
658 if ( !context[method] || typeof context[method] !== 'function' ) {
659 throw new Error( 'Method not found: ' + method );
660 }
661 // Resolve to function
662 callback = context[method];
663 } else {
664 callback = method;
665 }
666 this.off( event, callback, context );
667 }
668 } else {
669 // Remove all connections to the context
670 for ( event in this.bindings ) {
671 bindings = this.bindings[event];
672 i = bindings.length;
673 while ( i-- ) {
674 if ( bindings[i].context === context ) {
675 this.off( event, bindings[i].callback, context );
676 }
677 }
678 }
679 }
680
681 return this;
682 };
683 /**
684 * @class OO.Registry
685 * @mixins OO.EventEmitter
686 *
687 * @constructor
688 */
689 oo.Registry = function OoRegistry() {
690 // Mixin constructors
691 oo.EventEmitter.call( this );
692
693 // Properties
694 this.registry = {};
695 };
696
697 /* Inheritance */
698
699 oo.mixinClass( oo.Registry, oo.EventEmitter );
700
701 /* Events */
702
703 /**
704 * @event register
705 * @param {string} name
706 * @param {Mixed} data
707 */
708
709 /* Methods */
710
711 /**
712 * Associate one or more symbolic names with some data.
713 *
714 * Only the base name will be registered, overriding any existing entry with the same base name.
715 *
716 * @param {string|string[]} name Symbolic name or list of symbolic names
717 * @param {Mixed} data Data to associate with symbolic name
718 * @fires register
719 * @throws {Error} Name argument must be a string or array
720 */
721 oo.Registry.prototype.register = function ( name, data ) {
722 var i, len;
723 if ( typeof name === 'string' ) {
724 this.registry[name] = data;
725 this.emit( 'register', name, data );
726 } else if ( Array.isArray( name ) ) {
727 for ( i = 0, len = name.length; i < len; i++ ) {
728 this.register( name[i], data );
729 }
730 } else {
731 throw new Error( 'Name must be a string or array, cannot be a ' + typeof name );
732 }
733 };
734
735 /**
736 * Get data for a given symbolic name.
737 *
738 * Lookups are done using the base name.
739 *
740 * @param {string} name Symbolic name
741 * @return {Mixed|undefined} Data associated with symbolic name
742 */
743 oo.Registry.prototype.lookup = function ( name ) {
744 return this.registry[name];
745 };
746 /**
747 * @class OO.Factory
748 * @extends OO.Registry
749 *
750 * @constructor
751 */
752 oo.Factory = function OoFactory() {
753 oo.Factory.super.call( this );
754
755 // Properties
756 this.entries = [];
757 };
758
759 /* Inheritance */
760
761 oo.inheritClass( oo.Factory, oo.Registry );
762
763 /* Methods */
764
765 /**
766 * Register a constructor with the factory.
767 *
768 * Classes must have a static `name` property to be registered.
769 *
770 * function MyClass() {};
771 * // Adds a static property to the class defining a symbolic name
772 * MyClass.static = { 'name': 'mine' };
773 * // Registers class with factory, available via symbolic name 'mine'
774 * factory.register( MyClass );
775 *
776 * @param {Function} constructor Constructor to use when creating object
777 * @throws {Error} Name must be a string and must not be empty
778 * @throws {Error} Constructor must be a function
779 */
780 oo.Factory.prototype.register = function ( constructor ) {
781 var name;
782
783 if ( typeof constructor !== 'function' ) {
784 throw new Error( 'constructor must be a function, cannot be a ' + typeof constructor );
785 }
786 name = constructor.static && constructor.static.name;
787 if ( typeof name !== 'string' || name === '' ) {
788 throw new Error( 'Name must be a string and must not be empty' );
789 }
790 this.entries.push( name );
791
792 oo.Factory.super.prototype.register.call( this, name, constructor );
793 };
794
795 /**
796 * Create an object based on a name.
797 *
798 * Name is used to look up the constructor to use, while all additional arguments are passed to the
799 * constructor directly, so leaving one out will pass an undefined to the constructor.
800 *
801 * @param {string} name Object name
802 * @param {Mixed...} [args] Arguments to pass to the constructor
803 * @return {Object} The new object
804 * @throws {Error} Unknown object name
805 */
806 oo.Factory.prototype.create = function ( name ) {
807 var args, obj, constructor;
808
809 if ( !this.registry.hasOwnProperty( name ) ) {
810 throw new Error( 'No class registered by that name: ' + name );
811 }
812 constructor = this.registry[name];
813
814 // Convert arguments to array and shift the first argument (name) off
815 args = Array.prototype.slice.call( arguments, 1 );
816
817 // We can't use the "new" operator with .apply directly because apply needs a
818 // context. So instead just do what "new" does: create an object that inherits from
819 // the constructor's prototype (which also makes it an "instanceof" the constructor),
820 // then invoke the constructor with the object as context, and return it (ignoring
821 // the constructor's return value).
822 obj = Object.create( constructor.prototype );
823 constructor.apply( obj, args );
824 return obj;
825 };
826 /*jshint node:true */
827 if ( typeof module !== 'undefined' && module.exports ) {
828 module.exports = oo;
829 } else {
830 global.OO = oo;
831 }
832 }( this ) );