[팁] 자바스크립트 지역변수, 전역변수의 유효범위 (중요)
2015.08.26 20:43
310
0
https://eond.com/361316
object(HotopayModel)#862 (23) { ["error"]=> int(0) ["message"]=> string(7) "success" ["variables"]=> array(0) { } ["httpStatusCode"]=> int(200) ["module"]=> string(7) "hotopay" ["module_info"]=> NULL ["origin_module_info"]=> NULL ["module_config"]=> NULL ["module_path"]=> string(52) "/home/eond/web/eond.com/public_html/modules/hotopay/" ["xml_info"]=> NULL ["module_srl"]=> NULL ["mid"]=> NULL ["act"]=> NULL ["template_path"]=> NULL ["template_file"]=> NULL ["layout_path"]=> NULL ["layout_file"]=> NULL ["edited_layout_file"]=> NULL ["stop_proc"]=> bool(false) ["user"]=> object(Rhymix\Framework\Helpers\SessionHelper)#861 (20) { ["member_srl"]=> int(0) ["user_id"]=> NULL ["user_name"]=> NULL ["nick_name"]=> NULL ["email_address"]=> NULL ["phone_number"]=> NULL ["homepage"]=> NULL ["blog"]=> NULL ["birthday"]=> NULL ["denied"]=> NULL ["status"]=> NULL ["is_admin"]=> string(1) "N" ["is_site_admin"]=> NULL ["profile_image"]=> NULL ["image_name"]=> NULL ["image_mark"]=> NULL ["signature"]=> NULL ["description"]=> NULL ["group_list"]=> array(0) { } ["menu_list"]=> array(0) { } } ["request"]=> object(Rhymix\Framework\Request)#10 (13) { ["method"]=> string(3) "GET" ["compat_method"]=> string(3) "GET" ["url"]=> string(32) "javascript/361316/comment/441279" ["hostname"]=> string(8) "eond.com" ["domain"]=> NULL ["protocol"]=> string(5) "https" ["callback_function"]=> string(0) "" ["_route_status":protected]=> int(200) ["_route_options":protected]=> object(stdClass)#11 (5) { ["cache_control"]=> bool(true) ["check_csrf"]=> bool(true) ["is_forwarded"]=> bool(false) ["is_indexable"]=> bool(true) ["enable_session"]=> bool(true) } ["module"]=> string(5) "board" ["mid"]=> string(10) "javascript" ["act"]=> string(16) "dispBoardContent" ["args"]=> array(5) { ["page"]=> int(3) ["document_srl"]=> string(6) "361316" ["comment_srl"]=> string(6) "441279" ["mid"]=> string(10) "javascript" ["act"]=> string(16) "dispBoardContent" } } ["ajaxRequestMethod"]=> array(2) { [0]=> string(6) "XMLRPC" [1]=> string(4) "JSON" } ["gzhandler_enable"]=> bool(true) }

논리적 오류를 범하기 쉽다. 전역변수, 지역변수의 유효범위에 대해서 중요하게 짚어볼 필요가 있다.

<script type="text/javascript">
a = "global";
function test()
{
    console.log(a);
    var a = "local";
    console.log(a);
}
test();

/* 
    - 함수내에서 사용하는 변수선언은 가급적 가장 위에 선언하는게 좋다
    - 일반적으로는 전역변수인 "global"이 표시될것 같지만 자바스크립트의 지역변수는 함수 호출시 호출객체의
       속성이 선언되기 때문에 전역변수 "global"이 표시되지않고 지역변수 a 가 표시된다. 선언은 됐으나 값이 할당
       되지 않았기 때문에 "undefined"가 표시된다. 
    - 전역변수로 표시하기 위해서는 test 함수 아래 a를 window.a 로 하면 global이 표시된다.    
*/
</script>

라이믹스는 이온디와 함께

닫기

마이페이지

로그인을 해주세요

네이버로 로그인