![知识共享许可协议](https://csdnimg.cn/release/phoenix/images/creativecommons/80x15.png)
可能有些小伙伴不知道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 =>{
return (y=>{
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>
[TaskList] 省选前板子补完计划
[模板] 计算几何1(基础): 点/向量/线/圆/多边形/其他运算
网络流刷题日记
[模板] 网络流相关/最大流ISAP/费用流zkw
11/5/2018模拟 Problem C
11/1/2018模拟 Max
[模板] 笛卡尔树 && RMQ
bzoj1010-[HNOI2008]玩具装箱toy
[模板] 斜率优化
- 最新文章
-
ADO.NET入门教程(一) 初识ADO.NET
[Android]Plug-in com.android.ide.eclipse.adt was unable to load class com.android.ide
CentOS6.4下搭建hadoop2.2(64bit)注意事项
Oracle更改字符集
ORA-01950: 对表空间 'NAMETABLESPACE' 无权限
【转】sun.misc.BASE64Encoder找不到jar包的解决方法
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/LoopTag
CentOs修改主机名
[key]严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener(Spring配置异常)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
- 热门文章
-
通过url给action传中文参数乱码解决方案
java反射-使用反射来操纵方法
对List中每个对象元素按时间顺序排序
在SpringMvc中使用hibernate的hibernate-validator来进行校验入参,如@NotNull , @Length
Java正则表达式——验证手机号和电话号码
Java核心技术梳理-异常处理
【面试】迄今为止把同步/异步/阻塞/非阻塞/BIO/NIO/AIO讲的这么清楚的好文章(快快珍藏)
try-with-resources语句
SpringBoot读取配置文件中的数据到map和list
springboot读取.properties配置文件中的map和list类型配置参数