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;
}
// 简写为&#