본문 바로가기

메서드3

[Javascript] Method와 함수의 차이 메서드와 함수가 동일시되는 다른 언어와 달리 Javascript에서는 Method와 함수에 차이가 존재한다. 메서드 vs 함수 메서드와 함수는 호출 방식에 따라 다르다. 함수를 호출하는 객체가 있을 경우에 메서드라고 하고, 함수를 호출하는 객체가 없는 경우 함수라고 한다. let obj = { show1: function() { console.log('show1() 메서드 호출'); } } function show2() { console.log('show2() 함수 호출'); } obj.show1(); // 메서드 show2(); // 함수 위 예제에서 show1() 함수는 객체 obj의 프로퍼티이며, obj 객체를 통해 호출했으므로 메서드이다. 반면에 show2() 함수는 객체를 생성하지 않고 직접 호.. 2023. 8. 26.
[Javascript] 배열에서 특정 값 찾기 배열 속 값 탐색하기 indexOf let arr1 = ['apple', 'banana', 'peach', 'tomato']; console.log(arr1.indexOf('banana')); // 결과값: 2 console.log(arr1.indexOf('lemon')); // 결과값: -1 indexOf 메서드는 배열 안에서 특정 문자열에 해당하는 위치를 찾을 수 있다. 일치하면 값이 배열 속 문자열이 존재하는 위치를 나타내며, 일치하지 않으면 -1이 결과값으로 나타난다. find // 배열일 경우 const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // callback(element, index, array) array.find(v => v > 5); // 6 fin.. 2022. 3. 31.
[React] Bind는 대체 무엇이며, 언제 사용하는가 React 문서를 읽다가 bind 메서드를 발견했다. class Toggle extends React.Component { constructor(props) { super(props); this.state = {isToggleOn: true}; // 콜백에서 `this`가 작동하려면 아래와 같이 바인딩 해주어야 합니다. this.handleClick = this.handleClick.bind(this);// ?????? } handleClick() { this.setState(prevState => ({ isToggleOn: !prevState.isToggleOn })); } render() { return ( {this.state.isToggleOn ? 'ON' : 'OFF'} ); } } bind 메.. 2022. 3. 17.