一、let
- let 不能重复声明
// 报错:
let name = 'andy';
let name = 'red';
// 不会报错:
var name = 'andy';
var name = 'red';
- 块级作用域,只能在代码块里有效
// 报错:
{
let name = 'andy';
}
console.log(name);
// 不会报错:
{
var name = 'andy';
}
console.log(name);
- 不存在变量提升
// 返回 undefined
console.log(name);
var name = 'andy';
// 报错
console.log(name);
let name = 'andy';
- 不影响作用域链
// 返回 andy
{
let name = 'andy';
function fn() {
console.log(name);
}
fn();
}
- let 实践案例
点击盒子切换颜色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
div {
float: left;
margin-right: 10px;
width: 200px;
height: 100px;
border: 1px solid #c55;
}
</style>
<body>
<div></div>
<div></div>
<div></div>
</body>
<script>
let div = document.querySelectorAll('div');
for (let i = 0; i < div.length; i++) {
div[i].onclick = function() {
div[i].style.backgroundColor = 'pink';
// this.style.backgroundColor = 'pink';
}
}
</script>
</html>
二、const
用于声明常量,一般常量使用大写字母
- 必须赋初始值
// 报错
const NAME;
- 常量值不能修改
// 报错
const NAME = "andy";
NAME = 'red';
- 块级作用域
// 报错
{
const NAME = "andy";
}
console.log(NAME);
- 可以对数组和对象的元素修改
对于数组和对象的元素修改,不算做对常量的修改,不会报错。
// 返回 [ 'andy', 'red', 'tony' ]
const NAMES = ['andy', 'red'];
NAMES.push('tony');
console.log(NAMES);
三、解构赋值
ES6 允许按照一定模式从数组和对象中提取值,对变量进行赋值。
- 数组的解构
// 返回 green red blue
const COLOR = ['green', 'red', 'blue'];
let [g, r, b] = COLOR;
// 声明了三个变量,每个变量的值就是数组对应位置的值
// g = COLOR[0]; r = COLOR[1]; b = COLOR[2];
console.log(g, r, b);
- 对象的解构
// 输出
// andy 50
// 救赎之道,就在其中
const ANDY = {
name: 'andy',
age: '50',
salvation: function() {
console.log('救赎之道,就在其中');
}
}
let { name, age, salvation} = ANDY;
console.log(name, age);
salvation();
四、模板字符串
ES6 引入新的声明字符串的方式 ``
。
- 声明格式
// 返回 我是一个字符串 string
let str = `我是一个字符串`;
console.log(str, typeof str);
- 内容中可以直接出现换行符
// 正常输出
let li = `<ul>
<li>Andy</li>
<li>Red</li>
</ul>`;
console.log(li);
- 拼接字符串
// 输出 Andy向往自由
let name = 'Andy';
let freedom = `${name}向往自由`;
console.log(freedom);
五、简化对象写法
ES6 允许在大括号里面,直接写入变量和函数,作为对象的属性和方法。
let name = 'andy';
let salvation = function() {
console.log('救赎之道,就在其中');
}
const ANDY = {
name, // name: name;
salvation,
freedom() {
console.log('向往自由');
}
}
console.log(ANDY);
六、箭头函数
ES6 允许使用 =>
定义函数。
- 声明格式
// 返回 3
let fn = (a, b) => {
return a + b;
}
console.log(fn(1, 2));
- this 是静态的
this 始终指向函数声明时所在作用域下的 this 的值。
function getName() {
console.log(this.name);
}
let getName1 = () => {
console.log(this.name);
}
window.name = 'window';
const SCHOOL = {
name: 'SCHOOL'
}
getName(); // this 指向 window
getName1(); // this 指向 window
getName.call(SCHOOL); // this 指向 SCHOOL,改变了指向
getName1.call(SCHOOL); // this 指向 window,不改变指向
- 不能作为构造函数
// 报错
let Person = (name, age) => {
this.name = name,
this.age = age
}
let andy = new Person('andy', 50);
- 不能使用 arguments 变量
// 返回 [Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
function fnn() {
console.log(arguments);
}
fnn(1, 2, 3, 4, 5)
// 报错
let fn = () => {
console.log(arguments);
}
fn(1, 2, 3, 4, 5);
- 箭头函数的简写
- 当形参有且只有一个的时候可以省略小括号
// 返回 6
let add = n => {
return n + n;
}
console.log(add(3));
- 当代码体只有一条语句的时候省略花括号
// 返回 9
let pow = n => n * n;
console.log(pow(3));
- 箭头函数实践
- 点击盒子2秒后切换颜色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
div {
width: 200px;
height: 200px;
background-color: #963;
}
</style>
<body>
<div></div>
</body>
<script>
let div = document.querySelector('div');
div.addEventListener('click', function() {
// this 指向div
setTimeout(() => {
// 小括号内 this 静态
console.log(this);
this.style.backgroundColor = '#369';
}, 2000);
})
</script>
</html>
- 筛选偶数
// 返回 [ 6, 10, 100 ]
const ARR = [1, 6, 9, 10, 100, 25];
// filter 过滤器,返回符合条件的数组
// 回调函数,遍历数组, item是数组中的值
const RESULT = ARR.filter(item => item % 2 === 0); // 如果符合则是true
console.log(RESULT);
- 箭头函数使用场景
- 箭头函数适合与this无关的回调,如定时器,数组方法回调;
- 不适合与this有关的回调,如事件回调,对象方法。
七、参数初始值
ES6 允许给函数参数赋初始值
- 形参初始值
// 返回 NaN
function add(a, b, c) {
return a + b + c;
}
console.log(add(1, 2));
// 返回 3
function add(a, b = 0, c = 0) {
return a + b + c;
}
console.log(add(1, 2));
- 与解构赋值结合
// 返回 175.178.66.58 root 123456 3306
function connect({ host='127.0.0.1', username, password, port }) {
console.log(host, username, password, port);
}
connect({
host: '175.178.66.58',
username: 'root',
password: '123456',
port: 3306
})
八、rest 参数
ES6 引入 rest 参数,用于获取函数的实参,用来替代 arguments。
- rest 参数返回数组
- rest 参数返回数组,提高了对参数处理的灵活程度。
// 返回对象 [Arguments] { '0': 1, '1': 2, '2': 3 }
function data() {
console.log(arguments);
}
data(1, 2, 3)
// 返回数组 [ 1, 2, 3 ]
function date(...args) {
console.log(args);
}
date(1, 2, 3)
- rest 参数必须放在最后
// 返回
// 1
// 2
// [ 3, 4, 5, 6 ]
function fn(a, b, ...args) {
console.log(a);
console.log(b);
console.log(args);
}
fn(1, 2, 3, 4, 5, 6)
// 报错
function fn(...args, a, b) {
console.log(a);
console.log(b);
console.log(args);
}
fn(1, 2, 3, 4, 5, 6)
九、扩展运算符
- 将数组转换为逗号分隔的参数序列
扩展运算符能将数组转换为逗号分隔的参数序列。
// 返回数组 [Arguments] { '0': [ 'andy', 'red' ] }
const NAMES = ['andy', 'red'];
function shawshank() {
console.log(arguments);
}
shawshank(NAMES);
// 返回参数序列 [Arguments] { '0': 'andy', '1': 'red' }
const NAMES = ['andy', 'red'];
function shawshank() {
console.log(arguments);
}
shawshank(...NAMES); // 等同 shawshank('andy', 'red');
- 数组合并
// 返回 ['red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'black']
const RGB = ['red', 'green', 'blue'];
const CMYK = ['cyan', 'magenta', 'yellow', 'black'];
// const COLOR = RGB.concat(CMYK);
const COLOR = [...RGB, ...CMYK]
console.log(COLOR);
- 数组克隆
扩展运算符克隆数组是浅拷贝。
const RGB = ['red', 'green', 'blue'];
const COLOR = [...RGB];
console.log(COLOR);
- 将伪数组转换为真正的数组
// 返回 [ 'andy', 'red' ]
function shawshank() {
return arguments;
}
const RESULT = shawshank('andy', 'red');
console.log([...RESULT]);