zoukankan      html  css  js  c++  java
  • 闭包的学习

    引自 http://stackoverflow.com/questions/111102/how-do-javascript-closures-work

    Once upon a time:

    There was a princess...

    function princess() {

    She lived in a wonderful world full of adventures. She met her Prince Charming, rode around her world on a unicorn, battled dragons, encountered talking animals, and many other fantastical things.

        var adventures = [];
    
        function princeCharming() { /* ... */ }
    
        var unicorn = { /* ... */ },
            dragons = [ /* ... */ ],
            squirrel = "Hello!";

    But she would always have to return back to her dull world of chores and grown-ups.

        return {

    And she would often tell them of her latest amazing adventure as a princess.

            story: function() {
                return adventures[adventures.length - 1];
            }
        };
    }

    But all they would see is a little girl...

    var littleGirl = princess();

    ...telling stories about magic and fantasy.

    littleGirl.story();

    And even though the grown-ups knew of real princesses, they would never believe in the unicorns or dragons because they could never see them. The grown-ups said that they only existed inside the little girl's imagination.

    But we know the real truth; that the little girl with the princess inside...

    ...is really a princess with a little girl inside.

  • 相关阅读:
    「杂文」随想录
    「小说」妖精舞于废墟之上
    昨日之盛,明日之俗 ~ SDOI2021 退役记
    P6292 区间本质不同子串个数
    「杂文」生之重
    「闭门造车」二叉分块树
    「杂文」雨色的魔法(一)
    「笔记」斜率优化 DP
    「笔记」后缀数组
    NOIP 2020 AFO 记
  • 原文地址:https://www.cnblogs.com/hhjiang14/p/4513807.html
Copyright © 2011-2022 走看看