값으로서 콜백
값으로서 콜백(Callback)
값으로 사용될 수 있는 특성을 이용하면 함수의 인자로 함수를 전달할 수 있음 값으로 전달된 함수는 호출될 수 있기 때문에 이를 이용하면 함수의 동작을 완전히 바꿀 수 있음
function sortNumber(a,b){
return a-b;
}
var numbers = [20, 10, 9,8,7,6,5,4,3,2,1];
alert(numbers.sort(sortNumber));
//[1,2,3,4,5,6,7,8,9,10,20]
1. 비동기처리(Ajax : Asynchronous Javascript / XML)
시간이 오래 걸리는 작업이 있을 때 이 작업이 완료된 후에 처리해야 할 일을 콜백으로 지정하면 해당 작업이 끝났을 때 미리 등록한 작업을 실행하도록 할 수 있음
→ 일종의 ToDo List
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<script type="text/javascript">
$.get('./datasource.json.js', function(result){
console.log(result);
}, 'json');
</script>
</body>
</html>
Last updated
Was this helpful?