1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | (function(global){ var adun = {}; global.ADUN = global.adun = adun; adun.Class = function() { }; adun.Class.create = function(definition) { if(definition == null) { throw new Error('definition is undefined (adun.Class.create)'); } var prototype = new this(); // empty Object for(var name in definition) { prototype[name] = definition[name]; } var Class = function() { if(arguments[0] != '!ADUN.INIT' && this.init) { this.init.apply(this, arguments); } }; Class.prototype = prototype; Class.constructor = adun.Class; Class.extend = adun.Class.extend; return Class; }; adun.Class.extend = function(definition) { if(this.constructor != adun.Class) { throw new Error('constructor of this is not adun.Class (adun.Class.extend)'); } else if(definition == null) { throw new Error('definition is undefined (adun.Class.extend)'); } var _super = this.prototype; var prototype = new this('!ADUN.INIT'); for(var name in definition) { prototype[name] = typeof definition[name] === 'function' && typeof prototype[name] === 'function' ? (function(name, fn) { return function() { this._super = _super[name]; var ret = fn.apply(this, arguments); delete this._super; return ret; } })(name, definition[name]) : definition[name]; } var Class = function() { if(arguments[0] != '!ADUN.INIT' && this.init) { this.init.apply(this, arguments); } }; Class.prototype = prototype; Class.constructor = adun.Class; Class.extend = adun.Class.extend; return Class; }; })(window); var Person = ADUN.Class.create({ init: function(name) { this.name = name; } }); var Student = Person.extend({ init: function(name, age) { this._super(name); this.age = age; } }); var ME = Student.extend({ init: function(name, age, language) { this._super(name, age); this.language = language; } }); var me = new ME('아둔이', 25, 'javascirpt'); console.dir(me); | cs |
아둔.js라는 간단한 2d엔진을 개발중임 ㅇㅇ..
일단 첫번째 과제가 프로토타입을 이용한 클래스 비스무리 메서드를 만드는건데
adun.Class.create-> return 생성자 함수 // 초기 클래스 구현(오리지널)
생성자 함수.extend -> 상속
상속된 클래스는 오버라이딩이 가능한데 오리지널은 this._super() 로 메서드에서 접근.
여기까지는 됐는데..
문제는
constructor 프로퍼티가 보다시피 Class 임 난 adun.Class 로 뜨게하고싶음
-> 그러나 adun.Class로 할경우 원본이 손상됨..
어찌해야할까??
머싯네여