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