[JS] 12. scope

2017. 8. 28. 13:40·JS/JavaScript
728x90
반응형

12_Scope.html

<script>
// [Global Scope]
// In a browser, the global scope is the window object. 
// If a variable declaration occurs outside of a function, 
// then that variable exists on the global object.

// it could be referenced as window.x, 
// but because it exists on the global object we can simply refer to it as x.
var x = 9;

//[Local Scope]
//JavaScript also creates a Local Scope inside each function body
function myFunc(){
    var y=5;
}
//console.log(y); //ReferenceError: y is not defined

//if you declare a variable and forget to use var keyword.
//that variable is automatically made global.
(function(){
    z=10;
})();
console.log(z);

//Wrapping everything in a function which is then immediately invoked means 
//all the variables within that function are bound to the local scope. 
//At the very end you can then expose all your methods by binding 
//the jQuery object to the window, the global object. 
(function(){
    var jQuery = {/*All my methods go here */};
    window.jQuery = jQuery;
    
})();

function outer(){
    var x = 5;
    function inner(){
        console.log(x);
        var y = 10;
    }
    inner(); //5
    //console.log(y); // ReferenceError: y is not defined
}
outer();

//Functions have access to variables defined in the same scope.
var foo = "hello";
var.sayHello = function(){
    console.log(foo);
};
sayHello(); //"hello"
console.log(foo); //"hello"

//Variable with the same nama can exist in different scopes with different values:
var foo = "world";
var sayHello = function(){
    var foo = "hello";
    console.log(foo);
};
sayHello(); //"hello"
console.log(foo);//"world"

//When, within a function, you reference a variable defined in an outer scope, 
//that function can see changes to the variable's value after the function is defined.
var myFunction = function() {
    var foo = "hello";
    var myFn = function() {
    console.log( foo );
    };
    foo = "world";
    return myFn;
};
var f = myFunction();
f(); // "world"


(function() {
var baz = 1;
var bim = function() {
console.log( baz );
};
bar = function() {
console.log( baz );
};
})();
bar();
//bim(); //bim is not defined outside of the function
//console.log( baz ); // baz is not defined outside of the function
</script>
 

728x90
반응형

'JS > JavaScript' 카테고리의 다른 글

[JQuery] [ajax] 04. ajaxMethod  (0) 2017.08.28
[JS] 13. Closure  (0) 2017.08.28
[JS] 11. This  (0) 2017.08.28
[JS] 10. TypeOf  (0) 2017.08.28
[JS] 09. Function  (0) 2017.08.28
'JS/JavaScript' 카테고리의 다른 글
  • [JQuery] [ajax] 04. ajaxMethod
  • [JS] 13. Closure
  • [JS] 11. This
  • [JS] 10. TypeOf
밍글링글링
밍글링글링
mingling - 밍글링, 밍글밍글링. 코드와 어우러지다. IT/ 프로그래밍/소스
    반응형
    250x250
  • 밍글링글링
    mingling
    밍글링글링
  • 전체
    오늘
    어제
    • 밍글링글링 (463)
      • Flutter (2)
      • 일상생활 (8)
        • 리뷰 (1)
        • 생활정보 (4)
        • 맛집 (0)
        • 여행 (0)
        • 모든정보 (3)
      • JAVA (126)
        • 개념 (6)
        • 예제 (115)
        • Exception (2)
      • C (1)
        • C (1)
        • C++ (0)
        • C# (0)
      • JS (29)
        • JavaScript (18)
        • JQuery (5)
        • AJax (0)
        • NODE.JS (6)
        • Angular.JS 2.0 (0)
      • WEB (87)
        • HTML (6)
        • CSS (61)
        • JSP (20)
        • JSTL (0)
      • FrameWork (8)
        • Spring (8)
        • BootStrap (0)
        • MyBATIS (0)
        • JUnit (0)
      • 외부 라이브러리 (5)
      • 공유 소스 관리 (5)
        • Git (5)
        • SVN (0)
      • 빅데이터 프로그래밍 (37)
        • Python (37)
        • R Programming (0)
      • DB (7)
        • ORACLE (0)
        • MySql (6)
      • Development Tools (7)
        • StarUML (0)
        • eXERD (0)
        • Eclipse (4)
      • SKILL (6)
        • Migration (0)
        • Security (6)
      • MicroSoft (0)
        • Excel (0)
        • Word (0)
      • Android (0)
      • Server (21)
        • Ubuntu (5)
        • Linux (15)
      • IOS (0)
      • XML (0)
      • 미디어 (0)
      • 공지사항 (3)
      • NETWORK (1)
      • 게임 (4)
        • 피파 (1)
        • 리니지M (0)
        • 배틀그라운드 (1)
        • 듀랑고 (2)
      • 세상 이슈 (6)
      • 일렉트론 (0)
      • 대회 소식 (4)
      • 업무 (2)
      • Express, Vue (6)
      • docker (11)
      • svelte (3)
      • 블록체인 (1)
      • IT (66)
      • Rust (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    VS Code 팁
    css table
    자바 배열
    proxy pass
    mysql db
    vue cli
    nginx
    nginx ssl 적용
    API 설계
    css transition
    AI 반도체
    ubuntu
    Rust lang
    ssl 인증서 발급
    servlet class
    리눅스 설치
    jsp parameter
    lang rust
    vue 설치
    React Compiler
    docker
    nginx ssl 설정
    에디터 팁
    rust linux
    svelte
    오류
    자바 for문
    프런트엔드
    브라우저 자동화
    css perspective
    gitlab 설치
    SSL 인증서
    css tb
    러스트
    spring java
    제미나이
    vscode
    자바 생성자
    Extension Bisect
    티스토리 자동화
    css float
    Java Array
    자바 exception
    jsp include
    Node
    클론코딩
    java casting
    AI 코딩 에이전트
    자바 클래스
    자바 객체 지향
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
밍글링글링
[JS] 12. scope
상단으로

티스토리툴바