ES6新特性

ES6 新特性

let 和 const 命令

let 所声明的变量,只在 let 命令所在的代码块内有效

for(let i = 0; i < 5; i++){
   
console.log(i);
}
console.log("循环外:" + i)

在这里插入图片描述
const 声明的变量是常量,不能被修改,类似于java中final关键字

const a = 1;
console.log("a = ", a);
//给a重新赋值
a = 2;
console.log("a = ", a);

在这里插入图片描述

字符串扩展

<script>
let str = "hello heima";
console.log(str, " 中是否包含了heima => ", str.includes("heima"));
console.log(str, " 中是否包含了baima => ", str.includes("baima"));
console.log(str, " 中是否以h开头 => ", str.startsWith("h"));
console.log(str, " 中是否以a开头 => ", str.startsWith("a"));
console.log(str, " 中是否以a结束 => ", str.endsWith("a"));
console.log(str, " 中是否以h结束 => ", str.endsWith("h"));
</script>

在这里插入图片描述

<script>
let str = `
hello
itheima
itcast
`;
console.log(str);
</script>

在这里插入图片描述

解构表达式

数组解构

let arr = [1,2,3]
const [x,y,z] = arr;// x,y,z将与arr中的每个位置对应来取值
// 然后打印
console.log(x,y,z);
const [a] = arr; //只匹配1个参数
console.log(a);

在这里插入图片描述

对象解构

const person = {
   
name:"jack",
age:21,
language: ['java','js','css']
}
// 解构表达式获取值
const {
   name,age,language} = person;
// 打印
console.log(name);
console.log(age);
console.log(language);

在这里插入图片描述

函数优化

函数参数默认值

function add(a , b = 1) {
   
return a + b;
}
// 传一个参数
console.log(add(10));

箭头函数

var print = function (obj) {
   
console.log(obj);
}
// 简写为:
var print2 = obj => console.log(obj);

// 两个参数的情况:
var sum = function (a , b) {
   
return a + b;
}
// 简写为&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值