Source: class.js

  1. /**
  2. * Copyright (c) 2015 NAVER Corp.
  3. * egjs projects are licensed under the MIT license
  4. */
  5. eg.module("class", [eg], function(ns) {
  6. "use strict";
  7. /**
  8. *
  9. * A module used to implement an application in object-oriented programming style
  10. * @group egjs
  11. * @ko 애플리케이션을 객체지향 프로그래밍 방식으로 구현할 때 사용하는 모듈
  12. * @class
  13. * @name eg.Class
  14. *
  15. * @support {"ie": "7+", "ch" : "latest", "ff" : "latest", "sf" : "latest", "edge" : "latest", "ios" : "7+", "an" : "2.1+ (except 3.x)"}
  16. * @param {Object} def definition. Follow the rules under <a href="https://en.wikipedia.org/wiki/Literal_(computer_programming)">Literals of objects</a>. Note that "construct" is a key reserved as a constructor function. <ko>클래스를 정의하는 부분. <a href="https://en.wikipedia.org/wiki/Literal_(computer_programming)">객체 리터럴 규칙</a>을 따른다. 단, 'construct'는 생성자 함수로 예약된 키다</ko>
  17. * @param {Function} [def.construct] The constructor of the class <ko>클래스 생성자 함수 (Optional)</ko>
  18. *
  19. * @example
  20. var Some = eg.Class({
  21. //Class initialize
  22. "construct" : function(val){
  23. this.val = val;
  24. },
  25. "sumVal" : function(val) {
  26. return this.val + val;
  27. }
  28. });
  29. var some = new Some(5);
  30. some.sumVal(5);//10
  31. */
  32. ns.Class = function(def) {
  33. var typeClass = function typeClass() {
  34. if (typeof def.construct === "function") {
  35. def.construct.apply(this, arguments);
  36. }
  37. };
  38. typeClass.prototype = def;
  39. /**
  40. * Returns an instance of a class itself.
  41. * @ko 클래스 자신의 인스턴스를 반환한다.
  42. * @method eg.Class#instance
  43. * @return {eg.Class} An instance of a class itself<ko>클래스 자신의 인스턴스</ko>
  44. */
  45. typeClass.prototype.instance = function() {
  46. return this;
  47. };
  48. typeClass.prototype.constructor = typeClass;
  49. return typeClass;
  50. };
  51. /**
  52. * Extends a class.
  53. * @ko 클래스를 상속한다.
  54. * @static
  55. * @method eg.Class.extend
  56. * @param {eg.Class} oSuperClass Superclass <ko>상속하려는 클래스</ko>
  57. * @param {Object} def Class definition. Follow the rules under <a href="https://en.wikipedia.org/wiki/Literal_(computer_programming)">Literals of objects</a>. Note that "construct" is a key reserved as a constructor function. <ko>클래스를 정의하는 부분. <a href="https://en.wikipedia.org/wiki/Literal_(computer_programming)">객체 리터럴 규칙</a>을 따른다. 단, 'construct'는 생성자 함수로 예약된 키다.</ko>
  58. * @param {Function} [def.construct] The constructor of the class <ko>클래스 생성자 함수 (Optional)</ko>
  59. * @return {eg.Class} An instance of a new class <ko>새로 생성된 클래스의 인스턴스</ko>
  60. * @example
  61. var Some = eg.Class.extend(eg.Component,{
  62. "some" : function(){}
  63. })
  64. */
  65. ns.Class.extend = function(superClass, def) {
  66. var extendClass = function extendClass() {
  67. // Call a parent constructor
  68. superClass.apply(this, arguments);
  69. // Call a child constructor
  70. if (typeof def.construct === "function") {
  71. def.construct.apply(this, arguments);
  72. }
  73. };
  74. var ExtProto = function() {};
  75. ExtProto.prototype = superClass.prototype;
  76. //extendClass.$super = oSuperClass.prototype; //'super' is supported not yet.
  77. var extProto = new ExtProto();
  78. for (var i in def) {
  79. extProto[i] = def[i];
  80. }
  81. extProto.constructor = extendClass;
  82. extendClass.prototype = extProto;
  83. return extendClass;
  84. };
  85. });
comments powered by Disqus