분류 전체보기104 [클린코드 자바스크립트] 호이스팅 주의하기 호이스팅이란? 간단하게 말하면 런타임시에 선언이 최상단으로 끌어올려지는 현상을 말한다. 예를 들어 var로 선언한 변수가 초기화가 되어 있지 않다면 undefined로 최상단에 끌어올려지는 현상이다. (let, const도 호이스팅 되지만 var 처럼 선언과 동시에 undefined로 초기화 되지는 않는다. ) 발생되는 문제 호이스팅으로 인해 예상하지 못하는 문제가 발생할 수 있다. var sum; function sum() { return 7 + 5; } console.log(sum()); // 12 console.log(typeof sum) // function var sum; console.log(sum()); // 12 console.log(typeof sum) // function functio.. 2021. 11. 11. [클린코드 자바스크립트] 임시변수 제거하기 임시변수 제거하기 임시변수란? 스코프 안에서 전역변수 처럼 사용되는 변수를 일컫는다. function getDOMElement() { const result = {}; // 임시 변수 result.title = document.querySelector('.title'); result.content = document.querySelector('.content'); return result; } 당장은 이 임시변수가 큰 문제를 일으키진 않지만, 해당 함수가 커질 경우 임시변수가 스코프 내에서 전역 변수로 쓰여질 위험이 있고, 개발자들이 임시변수에 접근하여 조작하므로 인해 예상치 못한 사이드 이펙트를 야기할 수 있다. 이 부분을 예방하기 위해서는 함수를 크게 만들지 않고 하나의 일만 하도록 쪼개는 것이 가장.. 2021. 11. 11. 코드리뷰 삽질 사건의 발단 같은 팀원분의 코드리뷰를 하다가 아래와 같은 코드를 만났다. ... 생략 this.setState = nextState => { const {isLoading, nodes, selectedImageUrl, paths} = this.state this.state = nextState if (nodes !== this.state.nodes) { nodeItems.setState({ isRoot: this.state.isRoot, nodes: this.state.nodes }) } ... 생략 } 나는 일말의 고민도 하지 않고 자바스크립트의 객체(배열)를 일치 연산자로 비교할 경우 메모리의 주소값으로 비교하기 때문에 당현히 false로 평가가 된다고 생각하였고 (코드에서 비교하고 있는 nodes는.. 2021. 10. 8. (Codility) CountNonDivisible - Javascript 문제 You are given an array A consisting of N integers. For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors. For example, consider integer N = 5 and array A such that: A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 6 For the following elements: A[0] = 3, the non-divisors are: 2, 6,.. 2021. 7. 18. 이전 1 2 3 4 ··· 26 다음