반응형
eval() 함수는 공식 홈페이지에서 취약점을 소개하고 절대 사용하지 말 것을 강조한다.
대체 함수로는
new Function 이 있다.
커뮤니티 등 검색을 해보면 eval() 함수를 쓸 상황은 나오지 않는다고 한다.
function solution1(string) {
return eval(string);
}
function solution2(string) {
return new Function('return ' + string)();
}
const string = ['4 + 444', '1 - 2222', '5 * 123', '123/ 3567'];
for (let i = 0; i < string.length; i++) {
console.log('solution1', solution1(string[i]));
console.log('solution2', solution2(string[i]));
}
반응형
'JavaScript > 메소드' 카테고리의 다른 글
[javascript] 거듭제곱 확인하기 (0) | 2023.07.12 |
---|---|
[javascript] 정규식 ^ , + ,[], match() (0) | 2023.07.11 |
[javascirpt] trim, split, 정규식 + 공백 여러개 구분 (0) | 2023.07.02 |
[javascript] 특정 문자 기준으로 단어 쪼개기 split (0) | 2023.07.01 |
[javascript] 특정 문자 포함 단어 제거하기 (0) | 2023.07.01 |
댓글