1.所有匿名函数和回调函数,都可用箭头函数简写
<body>
<button id="butn">click me!</button>
<script>
/*var arr=[2,3,1,22,13,21];
arr.sort(function(a,b){return a-b;});
console.log(String(arr));
var str="we two who and who";
str=str.replace(/\b[a-z]/ig,function(kw){
return kw.toUpperCase();
})
console.log(str);
var i=0;
function timer(){
console.log(i++);
}
setInterval(timer,1000);
arr.forEach(function(elem,i,arr){
arr[i]=elem*2;
});
console.log(String(arr));
var arr2=arr.map(function(elem){
return elem/2;
})
console.log(String(arr2));
btn.οnclick=function(){
alert("Hello");
}*/
</script>
</body>
去function,改=>
var arr=[2,3,1,22,13,21];
arr.sort((a,b)=>{return a-b;});
console.log(String(arr));
var str="we two who and who";
str=str.replace(/\b[a-z]/ig,(kw)=>{
return kw.toUpperCase();
})
console.log(str);
var i=0;
function timer(){
console.log(i++);
}
setInterval(timer,1000);
arr.forEach((elem,i,arr)=>{
arr[i]=elem*2;
});
console.log(String(arr));
var arr2=arr.map((elem)=>{
return elem/2;
})
console.log(String(arr2));
butn.οnclick=()=>{
alert("Hello");
}
2如果函数体只有一句话,可省略{}
如果仅有的一句话还是return,可继续省略return
var arr=[2,3,1,22,13,21];
arr.sort((a,b)=> a-b);
console.log(String(arr));
var str="we two who and who";
str=str.replace(/\b[a-z]/ig,(kw)=>kw.toUpperCase());
console.log(str);
var i=0;
function timer(){console.log(i++)};
setInterval(timer,1000);
arr.forEach((elem,i,arr)=>arr[i]=elem*2);
console.log(String(arr));
var arr2=arr.map((elem)=>elem/2);
console.log(String(arr2));
butn.οnclick=()=>alert("Hello")
3. 如果只有一个参数,可省略()