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