basicclass.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * A class implementing basic features useful for other classes.
  8. *
  9. * @class BasicClass
  10. * @mixins Emitter
  11. */
  12. CKEDITOR.define( [ 'emittermixin', 'utils' ], function( EmitterMixin, utils ) {
  13. function BasicClass() {
  14. }
  15. // Injects the events API.
  16. utils.extend( BasicClass.prototype, EmitterMixin );
  17. /**
  18. * Creates a subclass constructor based on this class.
  19. *
  20. * The function to becuase a subclass constructor can be passed as `proto.constructor`.
  21. *
  22. * @static
  23. * @param {Object} [proto] Extensions to be added to the subclass prototype.
  24. * @param {Object} [statics] Extension to be added as static members of the subclass constructor.
  25. * @returns {Object} The subclass constructor.
  26. */
  27. BasicClass.extend = function( proto, statics ) {
  28. var that = this;
  29. var child = ( proto && proto.hasOwnProperty( 'constructor' ) ) ?
  30. proto.constructor :
  31. function() {
  32. that.apply( this, arguments );
  33. };
  34. // Copy the statics.
  35. utils.extend( child, this, statics );
  36. // Use the same prototype.
  37. child.prototype = Object.create( this.prototype );
  38. // Add the new prototype stuff.
  39. if ( proto ) {
  40. proto = utils.clone( proto );
  41. delete proto.constructor;
  42. utils.extend( child.prototype, proto );
  43. }
  44. return child;
  45. };
  46. return BasicClass;
  47. } );