cclass.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. /**
  17. * cclass is a javascript Class inheritance implementation
  18. *
  19. * Example Usage:
  20. * var A = cclass.create(function() {
  21. * this.instance_member = 'foo';
  22. * }, {
  23. * test: function() {;
  24. * // do something!!!
  25. * }});
  26. *
  27. * var B = A.extend(function() {
  28. * this.__proto__.constructor();
  29. * }, {
  30. * static_member: 'bob',
  31. * test: function(){
  32. * this.parent.test();
  33. * }
  34. * });
  35. *
  36. * var a = new B();
  37. * a.test();
  38. *
  39. */
  40. var cclass = (function($, undefined) {
  41. function extend(ext_fn, attrs) {
  42. function parent() {};
  43. function fn() {
  44. ext_fn.apply(this, arguments);
  45. };
  46. parent.prototype = this.prototype;
  47. fn.prototype = new parent();
  48. fn.prototype.constructor = fn;
  49. fn.extend = extend;
  50. $.extend(fn.prototype, attrs || {}, {
  51. parent: parent.prototype
  52. });
  53. return fn;
  54. }
  55. return {
  56. create: function(fn, attrs) {
  57. $.extend(fn.prototype, attrs || {});
  58. fn.extend = extend;
  59. fn.parent = undefined;
  60. return fn;
  61. },
  62. extend: extend
  63. };
  64. })($, undefined);