[JS] 13. Closure

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

13_Closure.html

<script>
//Closures are an extension of the concept of scope. With closures, 
//functions have access to variables that were available in the scope 
//where the function was created.

//Each function executed within the loop will reference
//the last value stored in i (5).
//This won't behave as we want it to - every 100 milliseconds, 5 will alert
for(var i=0; i<5; i++){
    setTimeout(function(){console.log(i);}, 100);
}

//Using a closure to create a new private scope
//fix: “close” the value of i inside createFunction, so it won't change
var createFunction = function(i){
    return function(){
        console.log(i);
    };
};
for(var i=0; i<5; i++){
    setTimeout(createFunction(i), 100);
}



//Using a closure to access inner and outer object instances simultaneously.
var outerObj = {
        outerFunction: function(){
            //Provide a reference to outerObj through innerFunction's closure
            var self = this;
            var innerObj = {
                    myName: "inner",
                    innerFunction: function(){
                        console.log(self.myName, this.myName); //"outer inner"
                    }
            };
            innerObj.innerFunction(); //"outer inner"
            console.log(this.myName); //"outer"
        }
};
outerObj.outerFunction();

//Let's manipulate "this" with a basic example.
var user = "johnsmith";
var module = {
        getUser: function(){
            return this.user;
        },
        user: "janedoe"
};
//module.getUser() is called where "module" is "this"
//and "module.user" is returned. 
//janedoe
console.log(module.getUser());

//let's now store a reference in the global version of "this"
//getUser() called, "this" is global, "user" is returned
//johnsmith
var getUser = module.getUser;
console.log(getUser());

//store a ref with "module" bound as "this"
var boundGetUser = getUser.bind(module);
//boundGetUser() called, "module" is "this" again, "module.user" returned.
//janedoe
console.log(boundGetUser());
</script>
 

728x90
반응형

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

[JSP] 파일업로드 로딩 화면 샘플 소스 DEMO  (0) 2018.04.02
[JQuery] [ajax] 04. ajaxMethod  (0) 2017.08.28
[JS] 12. scope  (0) 2017.08.28
[JS] 11. This  (0) 2017.08.28
[JS] 10. TypeOf  (0) 2017.08.28
'JS/JavaScript' 카테고리의 다른 글
  • [JSP] 파일업로드 로딩 화면 샘플 소스 DEMO
  • [JQuery] [ajax] 04. ajaxMethod
  • [JS] 12. scope
  • [JS] 11. This
밍글링글링
밍글링글링
mingling - 밍글링, 밍글밍글링. 코드와 어우러지다. IT/ 프로그래밍/소스
    반응형
    250x250
  • 밍글링글링
    mingling
    밍글링글링
  • 전체
    오늘
    어제
    • 밍글링글링 (407) N
      • 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 (10) N
      • Rust (0)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바