ch03_test15.js
function add(a, b, callback){ var result=a+b; callback(result); } add(10, 10 , function(result){ console.log('더하기 결과 (콜백함수 안에서) : ' + result) ; });
Command: node "C:/Program Files (x86)/Brackets/www/macaronics/ch03_test15.js"
Working directory: C:/Program Files (x86)/Brackets/www/macaronics/
더하기 결과 (콜백함수 안에서) : 20
Program exited with code 0
ch03_test16.js
function add( a, b, callback){ var result = a+b; callback(result); var history=function(){ return a+ ' + ' + b + ' = ' +result; }; return history; } var add_history = add(20, 20 , function(result){ console.log('더하기 결과 : ' + result); }); console.log('add_history 의 자료형 : ' + typeof(add_history)); console.log(add_history());
Command: node "C:/Program Files (x86)/Brackets/www/macaronics/ch03_test16.js"
Working directory: C:/Program Files (x86)/Brackets/www/macaronics/
더하기 결과 : 40
add_history 의 자료형 : function
20 + 20 = 40
Program exited with code 0
ch03_test17.js
//클로저 외부함수변수가 내부 에 접근했을때
// 그 변수가 그대로 유지되는 것을 클로저라 한다.
// count 가 현재 외분에서 4번 호출 되었는데, 계속 증가 되는 것을 볼 수 있다.
function add( a, b, callback){ var result = a+b; callback(result); //클로저 외부함수변수가 내부 에 접근했을때 // 그 변수가 그대로 유지되는 것을 클로저라 한다. // count 가 현재 외분에서 4번 호출 되었는데, 계속 증가 되는 것을 볼 수 있다. var count =0; var history=function(){ count +=1; return count + " : " + a+ ' + ' + b + ' = ' +result; }; return history; } var add_history = add(20, 20 , function(result){ console.log('더하기 결과 : ' + result); }); console.log('add_history 의 자료형 : ' + typeof(add_history)); console.log(add_history()); console.log(add_history()); console.log(add_history()); console.log(add_history());
Command: node "C:/Program Files (x86)/Brackets/www/macaronics/ch03_test17.js"
Working directory: C:/Program Files (x86)/Brackets/www/macaronics/
더하기 결과 : 40
add_history 의 자료형 : function
1 : 20 + 20 = 40
2 : 20 + 20 = 40
3 : 20 + 20 = 40
4 : 20 + 20 = 40
Program exited with code 0
댓글 ( 4)
댓글 남기기