Method of undefined

vue를 통하여 개발을 하다보면 console창에 아래와 같은 에러들이 종종 발생하는 경우를 보게된다.

TypeError: Cannot read property 'length' of undefined
TypeError: Cannot read property 'indexOf' of undefined
TypeError: Cannot read property 'replace' of undefined
...

하지만 막상 결과를 보면 제대로 동작이 되어짐을 알 수 있는데, 이러한 부분이 굉장히 거슬린다면 아래와 같은 방법을 통해 vue에게 명시해주면 해결된다. 예를 들어서 문자열 형태로 받은 date에서 ‘T’라는 글자를 공백으로 만들기 위해서는 아래와 같은 과정을 거친다.

function (date) {
    // 타입이 'string'인지 확인, 
    if (typeof date === 'string')
        // string이라고 명시가 되었을 때 replace 실행
        return date.replace('T', ' ');
    return date;
}