최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday

티스토리 뷰

 

 

 

 

 

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* --------------------------------------------------------
*
* Basic
*
-----------------------------------------------------------*/
var Basic = function($Control, $obj){
 
    //private
    var name = "Basic";
    var author = "serpiko";
    var ver = "2020.03.03";
 
  // bind
    this.nextShow = this.nextShow.bind(this);
 
    //public
    this.name = name;
  this.basicElement = this.inherit( BasicElement, this.basicElement ); //엘리먼트만 사용하기위해서 객체 상속
  this.control    = $Control;
  this.basicValidate = $Control.basicValidate;
  this.layerPopup = $Control.layerPopup;
  
    //extend
    this.defaultObj = {
    };
 
    for(var key in $obj) if( $obj.hasOwnProperty(key) ) this.defaultObj[key] = $obj[key];
 
    //build
}//end. Basic
 
Basic.prototype = ( () => {
    
    return{
        
        constructor: Basic,
        
        /******************************************************************************************************************
        *
        *    inherit : 상속 함수
        *
        ******************************************************************************************************************/
        inherit: function( Parent, Child ){
            Child = function(){
                Parent.call( this );
            }
            
            try{
                if (!Object.create) {
                    Object.create = (function(){
                        function F(){}
                            return function(o){
                            if (arguments.length != 1) {
                                throw new Error('Object.create 의 implementation은 하나의 파라메터만 허용합니다.');
                            }
                            F.prototype = o;
                            return new F();
                        }
                    })();
                }
                
                Child.prototype = Object.create( Parent.prototype );
                
                Child.prototype.constructor = Child;
                
                //override
                //Child.prototype.build = function(){ alert('hi, I am a Child'); };
                
                var child = new Child();
                if(child instanceof Parent === true
                    return child;
                else 
                    return new Parent();
 
            }catch(e){
                throw new Error( "[ inherit Error ] : "+ Parent.name +"객체를 상속받지 못했습니다. "+ Parent.name +" 객체를 '확인'해 주세요." );
            }
        },
        
 
        /******************************************************************************************************************
        *
        *    constructor
        *
        ******************************************************************************************************************/
        build(){
            iftypeof this.control == "undefined" || this.control.name != "Control"  ){
                throw new Error( "["+this.name+"] : Control 객체를 상속받지 못했습니다. Control 객체를 먼저 '생성'해 주세요." );
                return false;
      }
      
      //( () => this.stepClickHandler() )();
      //( () => this.addEventListener() )();
      this.stepClickHandler();
      this.addEventListener();
    },
        
         stepClickHandler(){
      
      $(".step-btn").on("click touchstart", e => {
        e.stopPropagation();
        e.preventDefault();
        
        
        switchthis.model.currentStep ){
 
          // 시작하기 => 반려견 선택 => 이름
          case 0:
          break;
 
          case 1:
            if( this.basicValidate.validateName() ) this.nextShow();
          break;
 
          // 견종 => 생일
          case 2:
                      //if( this.basicValidate.validateBreed() ) this.prepareBirthday( this.nextShow ); // undefined
                        if( this.basicValidate.validateBreed() ) this.prepareBirthday( () => this.nextShow() );
          break;
                }
            });
        },
        
        nextShow( callback ){
            
            console.log(this);
 
      $(".step-"+this.model.prevStep).hide();
      this.model.currentStep++;
 
 
      if"undefined" !== typeof(callback) ) {
                if"function" == typeof(callback) ) callback();
                else throw new Error('callback is not a function!');
            }
        $(".step-"+this.model.currentStep).fadeIn('slow'function(){
      });
    },
        
        addEventListener(){
      $("#modalPetListConfirmBtn").click( e => {
                const pet_idx = $("input[name='pet_list']:checked").val();
        
        if!pet_idx ) this.nextShow();
        else window.location.href = `${this.model.szEatStartMealUrl}?pet_idx=${pet_idx}&header=0&category=0`;
      });
 
      $("#modalPetListCloseBtn").click( e=>{
        this.nextShow();
      });
    },
        
    }
    
});
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

15 : 처럼 직접 bind 하거나

 

124 : 처럼 유실된 컨텍스트를 arrow function을 사용하여 동일한 생성자의 인스턴스를 참조하므로 this는 Basic 객체를 가르킨다.

 

참고로 jquery 이벤트 핸들러의 this는 버튼엘리먼트 객체가 아니므로 event.currentTarget을 제이쿼리 셀렉터로 캡쳐해서 $(e.currentTarget) 방식으로 사용..

 

 

 

 

 

source : https://www.freecodecamp.org/news/this-is-why-we-need-to-bind-event-handlers-in-class-components-in-react-f7ea1a6f93eb/

 

댓글