ES6 基础实践
let 和 const
let 和 var 的区别
var a = 1;
window.a; // 输出:1
let b = 2;
window.b; // 输出:undefined
{
let a = 1;
// let a = 2;
var b = 2;
}
console.log(a);
console.log(b);
let a = 1;
{
console.log(a);
let a = 2;
}
let i = "a";
for(let i = 0; i < 10; i++){
// let i = "b";
console.log(i);
}
// 猜猜是否报错,以及输出什么
const 常量
const a = [1,2,3];
const b = a;
const c = [1,2,3];
模板字符串
普通字符串
`我是一个好人`
多行字符串
console.log(`我还是可以
换行的字符串`);
let a = `1
2
3
4`;
a.length //猜猜a的长度是多少?
let b = "1\n2\n3\n4";
a === b //猜猜是否相等?
插入变量
let name = "张三";
let age = 12;
function say(name){
return "你好,很高兴认识你," + name;
}
console.log(`我是${name},我的性别是:${ "男" },我今年 ${ age * 2 }岁,我想说: ${ say("李四") }`);
延伸思考
let a = "abc";
let b = new String("abc");
a == b?
a === b?
解构赋值
数组解构
let a = 1, b = 2, c = 3;
let [a, b, c] = [1, 2, 3];
let [a, ...b] = [1, 2, 3, 4];
let [a = 1] = [2];
let [a = 1] = [null];
let [a = 1] = [undefined];
let [a] = [null];
let [a] = [];
对象解构
let {a, b} = {a: 1, b: 2 };
let {a : newA, b: newB} = {a: 1, b: 2 };
let {a = 1} = {};
let {a = 1} = {a:2};
1、先两句一起执行:
let a = 1;
{a} = {a: 2}
2、分开一条一条执行
let a = 1;
{a} = {a: 2}
3、反过来执行
{a} = {a: 2}
let a = 1;
a ?
window.a ?
上面的a 和 window.a 各输出什么?
let a = 1;
({a} = {a: 2})
如果这样的两句一起执行呢?
数组和对象解构?
用途
- 交换变量的值
let a = 1;
let b = 2;
[a, b] = [b, a];
- 从函数返回多个值
function example() {
return [1, 2, 3];
}
let [a, b, c] = example();
function example() {
return {
a: 1,
b: 2
};
}
let {a, b} = example();
- 函数参数的定义
function example([a, b, c]) {
console.log(a, b, c);
}
example([1, 2, 3]);
- 提取JSON数据
let myDataList = {
name: "张三",
sex: "男",
age: "24"
};
let { name, sex, age} = myDataList;
- 函数参数的默认值
let throwMyErr = (errName) => {
throw new Error(`请传入参数 ${errName}`);
}
let example = (name = throwMyErr("name")) => {
console.log(name);
}
箭头函数
let example = x => x * 2;
let myObj = {
funA : function(){
setTimeout(function(){
console.log(this);
}, 500);
}
}
let myObj = {
funA : function(){
setTimeout(() => {
console.log(this);
}, 500);
}
}
class和extends
function SayHellowES5 (){
this.name = "SARS";
}
SayHellowES5.prototype.say = function () {
console.log(this.name);
};
class SayHellow {
constructor() {
this.name = "SARS";
}
say(){
console.log(this.name);
}
}
typeof SayHellow
SayHellow === SayHellow.prototype.constructor
var a = new SayHellow();
a === SayHellow ?
a.constructor === SayHellow ?
var b = new SayHellow();
a.__proto__ === b.__proto__ ?
原型是否相等?
b.__proto__.say2 = function(){
alert("say2");
}
a.say2() ?
思考 this 到底指的哪?原型呢?同样下面的继承是否也一样?
(确认下节课内容)
class SayBey extends SayHellow{
sayBey(){
console.log("beybey!");
}
}
class toBey extends SayHellow{
sayBey(){
console.log(`${this.name} beybey!`);
}
}
class toBey2 extends SayHellow{
constructor() {
this.sex = "女";
}
sayBey(){
console.log(`${this.sex} beybey!`);
}
}
//想想为什么报错?
模块化 export import
导出模块
export let myLetA = 1;
export let myLetB = 2;
export let example = () => {
alert("SARS");
}
let myLetA = 1;
let myLetB = 2;
let example = () => {
alert("SARS");
}
export {myLetA, myLetB, example as name};
// 正确
var a = 1;
export default a;
// 错误
export default var a = 1;
导入模块
import {myLetA, myLetB, example as name} from ‘./main‘;
import SARS from ‘./main‘;