Proxy.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Proxy
  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. * @module CreateJS
  30. */
  31. // namespace:
  32. this.createjs = this.createjs||{};
  33. /**
  34. * Various utilities that the CreateJS Suite uses. Utilities are created as separate files, and will be available on the
  35. * createjs namespace directly:
  36. *
  37. * <h4>Example</h4>
  38. * myObject.addEventListener("change", createjs.proxy(myMethod, scope));
  39. *
  40. * @class Utility Methods
  41. * @main Utility Methods
  42. */
  43. (function() {
  44. "use strict";
  45. /**
  46. * A function proxy for methods. By default, JavaScript methods do not maintain scope, so passing a method as a
  47. * callback will result in the method getting called in the scope of the caller. Using a proxy ensures that the
  48. * method gets called in the correct scope.
  49. *
  50. * Additional arguments can be passed that will be applied to the function when it is called.
  51. *
  52. * <h4>Example</h4>
  53. * myObject.addEventListener("event", createjs.proxy(myHandler, this, arg1, arg2));
  54. *
  55. * function myHandler(arg1, arg2) {
  56. * // This gets called when myObject.myCallback is executed.
  57. * }
  58. *
  59. * @method proxy
  60. * @param {Function} method The function to call
  61. * @param {Object} scope The scope to call the method name on
  62. * @param {mixed} [arg] * Arguments that are appended to the callback for additional params.
  63. * @public
  64. * @static
  65. */
  66. createjs.proxy = function (method, scope) {
  67. var aArgs = Array.prototype.slice.call(arguments, 2);
  68. return function () {
  69. return method.apply(scope, Array.prototype.slice.call(arguments, 0).concat(aArgs));
  70. };
  71. }
  72. }());