zoukankan      html  css  js  c++  java
  • JavaScript ES6和ES5闭包的小demo

    知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

    可能有些小伙伴不知道ES6的写法,这儿先填写一个小例子

     
                let connter = (res => {
                    for(var count = 1; ; count++){
                        console.log(count+'A');
                        if(count ===5){
                            return;
                        }
                        console.log(count+'B');
                    }
                })(0)
    
        <div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul></pre>
        <p>注意:这儿采用了箭头函数。</p>
        <p>下面我们正式找出不一样:</p>
        <pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)">
        // ES6闭包
        let makeAdder =(x =&gt;{
            return (y=&gt;{
                return x+y;
            })  
        })
    
    
        var add5 = makeAdder(5);
        console.log(add5(2));//7
    
        var add6 = makeAdder(10);
        console.log(add10(2));//12
        <div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li></ul></pre>
        <p>下面是ES5的闭包</p>
        <pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)">
        //es5语法闭包
        function makeAdder2(x) {
            return function(y) {
            return x + y;
            };
        }
        
    
        var add8 = makeAdder2(8);
        console.log(add8(2));//10
    
    
        var add9 = makeAdder2(10);
        console.log(add8(2));//12
    
    
        <div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li></ul></pre>
        <p>从上面可以看出,输出的过值都是一样的,但ES6使代码更简洁,更具有高效性,在性能上超过ES5。</p>
    
                                            </div>
                        <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
                            </div>
  • 相关阅读:
    js的event对象 详解
    RestSharp使用详解(1)调用阿里巴巴开放存储服务
    RestSharp使用详解(2)RestSharp的BUG和不足
    WF实例学习笔记:(2)通过Workflow 调用 WCF Data Services 获取数据
    译文:SQL Azure客户端瞬态错误处理最佳实践
    Windbg 基本命令
    RestSharp使用详解(3)OSS文件上传的问题
    Transient Fault Handling and Retry Logic: 瞬间错误处理——重试
    推荐一本免费的Node.js电子书(台湾)
    CSS导航菜单应用滑动门技术的玻璃效果菜单
  • 原文地址:https://www.cnblogs.com/XSdao/p/11296576.html
Copyright © 2011-2022 走看看