EaselJS v0.8.2 API Documentation : createjs/utils/promote.js

API Documentation for: 0.8.2
Show:

File:promote.js

  1. /*
  2. * promote
  3. * Visit http://createjs.com/ for documentation, updates and examples.
  4. *
  5. * Copyright (c) 2010 gskinner.com, inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28.  
  29. /**
  30. * @module CreateJS
  31. */
  32.  
  33. // namespace:
  34. this.createjs = this.createjs||{};
  35.  
  36. /**
  37. * @class Utility Methods
  38. */
  39.  
  40. /**
  41. * Promotes any methods on the super class that were overridden, by creating an alias in the format `prefix_methodName`.
  42. * It is recommended to use the super class's name as the prefix.
  43. * An alias to the super class's constructor is always added in the format `prefix_constructor`.
  44. * This allows the subclass to call super class methods without using `function.call`, providing better performance.
  45. *
  46. * For example, if `MySubClass` extends `MySuperClass`, and both define a `draw` method, then calling `promote(MySubClass, "MySuperClass")`
  47. * would add a `MySuperClass_constructor` method to MySubClass and promote the `draw` method on `MySuperClass` to the
  48. * prototype of `MySubClass` as `MySuperClass_draw`.
  49. *
  50. * This should be called after the class's prototype is fully defined.
  51. *
  52. * function ClassA(name) {
  53. * this.name = name;
  54. * }
  55. * ClassA.prototype.greet = function() {
  56. * return "Hello "+this.name;
  57. * }
  58. *
  59. * function ClassB(name, punctuation) {
  60. * this.ClassA_constructor(name);
  61. * this.punctuation = punctuation;
  62. * }
  63. * createjs.extend(ClassB, ClassA);
  64. * ClassB.prototype.greet = function() {
  65. * return this.ClassA_greet()+this.punctuation;
  66. * }
  67. * createjs.promote(ClassB, "ClassA");
  68. *
  69. * var foo = new ClassB("World", "!?!");
  70. * console.log(foo.greet()); // Hello World!?!
  71. *
  72. * @method promote
  73. * @param {Function} subclass The class to promote super class methods on.
  74. * @param {String} prefix The prefix to add to the promoted method names. Usually the name of the superclass.
  75. * @return {Function} Returns the subclass.
  76. */
  77. createjs.promote = function(subclass, prefix) {
  78. "use strict";
  79.  
  80. var subP = subclass.prototype, supP = (Object.getPrototypeOf&&Object.getPrototypeOf(subP))||subP.__proto__;
  81. if (supP) {
  82. subP[(prefix+="_") + "constructor"] = supP.constructor; // constructor is not always innumerable
  83. for (var n in supP) {
  84. if (subP.hasOwnProperty(n) && (typeof supP[n] == "function")) { subP[prefix + n] = supP[n]; }
  85. }
  86. }
  87. return subclass;
  88. };
  89.