함수 바깥에서 변수를 선언하면 어느 함수에서든 변수를 사용할 수 있다. addProduct 함수에서 targetId 라는 변수에 데이터를 전달하므로, targetId 변수에는 항상 가장 최근에 추가한 관심 상품 데이터가 들어있다.
let targetId;
function addProduct(itemDto) {
$.ajax({
type:"POST",
url:"/api/products",
data: JSON.stringify(itemDto),
contentType: "application/json",
success: function (response){
$('#container').addClass('active');
targetId = response.id;
}
})
}