目录
const 定义常量
// 声明常量
const SCHOOL='尚硅谷';
特点:
1.一定要赋初始值
const A;
//会报错
2.一般常量使用大写(潜规则)
SCHOOL='ATGUIGU';
3.常量的值不能修改
SCHOOL='123'
4.块级作用域
{
const PLAYER='UZI';
}
console.log(PALYER);
//会报错
5.对于数组和对象的元素修改,不算对常量的修改,不会报错
原因:没有对常量地址发生改变
const TEAM=['UZI','111','222','333'];
TEAM.push('666');
变量的解构赋值
ES6允许一定模式从数组和对象中提取值,对变量进行赋值
1.数组的解构
const F4=['小沈阳','刘能','赵四','宋小宝'];
let [xiao,liu,zhao,song]=F4;
console.log(xiao);
console.log(liu);
console.log(zhao);
console.log(song);
2.对象的解构
const zhao1={
name:'赵本山',
age:'不详',
xiaopin:function(){
console.log("我可以演小品");
}
};
let {name,age,xiaopin}=zhao1;
console.log(name);
console.log(age);
console.log(xiaopin);
用对象解构较多,而且方法能够正常调用
xiaopin();
就不用写zhao1.xiaopin(),把xiaopin解构出来就好
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui:ios">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>变量的解构赋值</title>
<link rel="stylesheet" href="">
</head>
<body>
<script>
// ES6允许一定模式从数组和对象中提取值,对变量进行赋值
// 1.数组的解构
const F4=['小沈阳','刘能','赵四','宋小宝'];
let [xiao,liu,zhao,song]=F4;
console.log(xiao);
console.log(liu);
console.log(zhao);
console.log(song);
// 2.对象的解构
const zhao1={
name:'赵本山',
age:'不详',
xiaopin:function(){
console.log("我可以演小品");
}
};
let {name,age,xiaopin}=zhao1;
console.log(name);
console.log(age);
console.log(xiaopin);
// 方法能够正常调用
// 就不用写zhao1.xiaopin(),把xiaopin解构出来就好
xiaopin();
</script>
</body>
</html>