JavaScript——ES6新特性(1)

一、let

  1. let 不能重复声明
// 报错:
let name = 'andy';
let name = 'red';
// 不会报错:
var name = 'andy';
var name = 'red';
  1. 块级作用域,只能在代码块里有效
// 报错:
{
    let name = 'andy';
}
console.log(name);

// 不会报错:
{
    var name = 'andy';
}
console.log(name);
  1. 不存在变量提升
// 返回 undefined
console.log(name);
var name = 'andy';

// 报错
console.log(name);
let name = 'andy';
  1. 不影响作用域链
// 返回 andy
{
    let name = 'andy';
    function fn() {
        console.log(name);
    }
    fn();
}
  1. 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

用于声明常量,一般常量使用大写字母

  1. 必须赋初始值
// 报错
const NAME;
  1. 常量值不能修改
// 报错
const NAME = "andy";
NAME = 'red';
  1. 块级作用域
// 报错
{
    const NAME = "andy";
}
console.log(NAME);
  1. 可以对数组和对象的元素修改
    对于数组和对象的元素修改,不算做对常量的修改,不会报错。
// 返回 [ 'andy', 'red', 'tony' ]
const NAMES = ['andy', 'red'];
NAMES.push('tony');
console.log(NAMES);

三、解构赋值

ES6 允许按照一定模式从数组和对象中提取值,对变量进行赋值。

  1. 数组的解构
// 返回 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);
  1. 对象的解构
// 输出
// andy 50
// 救赎之道,就在其中
const ANDY = {
    name: 'andy',
    age: '50',
    salvation: function() {
        console.log('救赎之道,就在其中');
    }
}

let { name, age, salvation} = ANDY;
console.log(name, age);
salvation();

四、模板字符串

ES6 引入新的声明字符串的方式 ``

  1. 声明格式
// 返回 我是一个字符串 string
let str = `我是一个字符串`;
console.log(str, typeof str);
  1. 内容中可以直接出现换行符
// 正常输出
let li = `<ul>
<li>Andy</li>
<li>Red</li>
</ul>`;
console.log(li);
  1. 拼接字符串
// 输出 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 允许使用 => 定义函数。

  1. 声明格式
// 返回 3
let fn = (a, b) => {
    return a + b;
}
console.log(fn(1, 2));
  1. 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,不改变指向
  1. 不能作为构造函数
// 报错
let Person = (name, age) => {
    this.name = name,
        this.age = age
}
let andy = new Person('andy', 50);
  1. 不能使用 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);
  1. 箭头函数的简写
  • 当形参有且只有一个的时候可以省略小括号
// 返回 6
let add = n => {
    return n + n;
}
console.log(add(3));
  • 当代码体只有一条语句的时候省略花括号
// 返回 9
let pow = n => n * n;
console.log(pow(3));
  1. 箭头函数实践
  • 点击盒子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);
  1. 箭头函数使用场景
  • 箭头函数适合与this无关的回调,如定时器,数组方法回调;
  • 不适合与this有关的回调,如事件回调,对象方法。

七、参数初始值

ES6 允许给函数参数赋初始值

  1. 形参初始值
// 返回 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));
  1. 与解构赋值结合
// 返回 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。

  1. 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)
  1. 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)

九、扩展运算符

  1. 将数组转换为逗号分隔的参数序列
    扩展运算符能将数组转换为逗号分隔的参数序列。
// 返回数组 [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');
  1. 数组合并
// 返回 ['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);
  1. 数组克隆
    扩展运算符克隆数组是浅拷贝。
const RGB = ['red', 'green', 'blue'];
const COLOR = [...RGB];
console.log(COLOR);
  1. 将伪数组转换为真正的数组
// 返回 [ 'andy', 'red' ]
function shawshank() {
    return arguments;
}
const RESULT = shawshank('andy', 'red');
console.log([...RESULT]);

十、后接 ES6 新特性(2)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iFulling

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值