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 | (function() { 'use strict'; var Class = ADUN.Class = function(definition) { if(definition == null) { throw new Error('definition is undefined (adun.Class)'); } var name, extend = definition.extend || false, // 부모 클래스 참조 prototype = {}; // 프로토타입 -> 빈 객체 // 부모 클래스가 참조되어 있다면 블록에 진입한다. if( extend && ADUN.Utils.isObject(extend) ) { if( extend.constructor != adun.Class ) { throw new Error('extend constructor is not adun.Class (adun.Class)'); } // 프로토타입 -> 부모 클래스의 인스턴스 // 생성자 init을 실행하지 않는다. prototype = new extend('!ADUN.INIT'); } for ( name in definition ) { prototype[name] = ( ADUN.Utils.isFunction(definition[name]) && adun.Utils.isFunction(prototype[name]) ) ? // 부모 클래스에 같은 메서드가 있을 경우 블록에 진입한다.(오버라이딩) (function(name, fn) { return function() { // super 프로퍼티를 통해 부모 클래스의 메서드를 참조한다. this.super = extend.prototype[name]; var ret = fn.apply(this, arguments); delete this.super; return ret; } })(name, definition[name]) : definition[name]; } var Class = function() { if(this.init && arguments[0] !== '!ADUN.INIT') { // 생성자 init 함수를 실행한다. this.init.apply(this, arguments); } }; Class.prototype = prototype; Class.constructor = ADUN.Class; return Class; } })(); var Parent = ADUN.Class({ init: function(name) { this.naem = name; } }); var Child = ADUN.Class({ extend: Parent, init: function(name, age) { this.super(name); this.age = age; } }); | cs |
ㅇㅅㅇ 문제점이있을까요?
넹,,,
프로토타입 상속은 항상 다중상속이 문제던데
뻘짓말고 es6 합시다
외쳐! ES6!!!