1.2、let 和 const 命令
var
之前,我们写js定义变量的时候,只有一个关键字: var var 有一个问题,就是定义的变量有时会莫名奇妙的成为全局变量。
let
let 所声明的变量,只在 let 命令所在的代码块内有效。
const
const 声明的变量是常量,不能被修改,类似于java中final关键字。
在ES6中,为字符串扩展了几个新的API:
- includes() :返回布尔值,表示是否找到了参数字符串。
- startsWith() :返回布尔值,表示参数字符串是否在原字符串的头部。
- endsWith() :返回布尔值,表示参数字符串是否在原字符串的尾部。
1.3、解构表达式
1.3.1、数组解构
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);
1.3.2、对象解构
const person = {
name:"jack",
age:21,
language: ['java','js','css']
}
// 解构表达式获取值
const {name,age,language} = person;
// 打印
console.log(name);
console.log(age);
console.log(language);
1.4.1、函数参数默认值
//普通写法
function add(a , b) {
//判断b是否为空,为空就给默认值1 b = b || 1;
return a + b;
}
//传一个参数
console.log(add(10));
//ES6写法
function add(a , b = 1) {
return a + b;
}
// 传一个参数
console.log(add(10));
1.4.2、箭头函数
//一个参数时情况:
var print = function (obj) {
console.log(obj);
}
//简写为:
var print2 = obj => console.log(obj);
//两个参数的情况:
var sum = function (a , b) {
return a + b;
}
//简写为:
var sum2 = (a,b) => a+b;
//没有参数时,需要通过()进行占位,代表参数部分
let sayHello = () => console.log("hello!");
sayHello();
//代码不止一行,可以用 {} 括起来。
var sum3 = (a,b) => {
return a + b;
}
//多行,没有返回值
let sayHello = () => {
console.log("hello!");
console.log("world!");
}
sayHello();
1.4.3、对象的函数属性简写
let person = {
name: "jack",
//以前:
eat: function (food) {
console.log(this.name + "在吃" + food);
},
//箭头函数版:
eat2: food => console.log(person.name + "在吃" + food),// 这里拿不到this
//简写版:
eat3(food){
console.log(this.name + "在吃" + food);
}
}
1.4.4、箭头函数结合解构表达式
const person = {
name:"jack",
age:21,
language: ['java','js','css']
}
function hello(person) {
console.log("hello," + person.name)
}
如果用箭头函数和解构表达式
var hi = ({name}) => console.log("hello," + name);
hi(person)
1.5.1、map
let arr = ['1','20','-5','3'];
console.log(arr)
let newArr = arr.map(s => parseInt(s));
console.log(newArr) ///[1,20,-5,3]
1.5.2、reduce
const arr = [1,20,-5,3]
arr.reduce((a,b)=>a+b)///求和19
arr.reduce((a,b)=>a+b,1)///求和,初始值为1,结果20
1.6、扩展运算符
扩展运算符(spread)是三个点(…), 将一个数组转为用逗号分隔的参数序列 。
用法:
console.log (...[1, 2, 3]); //1 2 3
console.log(1, ...[2, 3, 4], 5); // 1 2 3 4 5
function add(x, y) {
return x + y;
}
var numbers = [1, 2];
console.log(add(...numbers)); // 3
//数组合并
let arr = [...[1,2,3],...[4,5,6]];
console.log(arr); //[1, 2, 3, 4, 5, 6]
//与解构表达式结合
const [first, ...rest] = [1, 2, 3, 4, 5];
console.log(first, rest) //1 [2, 3, 4, 5]
//将字符串转成数组
console.log([...'hello']) //["h", "e", "l", "l", "o"]
1.7、Promise
所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果。
const p = new Promise(function (resolve, reject) {
//这里我们用定时任务模拟异步 setTimeout(() => {
const num = Math.random();
//随机返回成功或失败
if (num < 0.5) {
resolve("成功!num:" + num)
} else {
reject("出错了!num:" + num)
}
}, 300);
// 调用promise
p.then(function (msg) {
console.log(msg);
}).catch(function (msg) {
console.log(msg);
})
1.8、set和map
//Set构造函数可以接收一个数组或空 let set = new Set();
set.add(1);// [1]
//接收数组
let set2 = new Set([2,3,4,5,5]);// 得到[2,3,4,5]
///方法:
set.add(1);// 添加
set.clear();// 清空
set.delete(2);// 删除指定元素
set.has(2); // 判断是否存在
set.forEach(function(){})//遍历元素
set.size; // 元素个数。是属性,不是方法。
//map接收一个数组,数组中的元素是键值对数组
const map = new Map([
['key1','value1'], ['key2','value2'],
])
//或者接收一个set
const set = new Set([
['key1','value1'], ['key2','value2'],
])
const map2 = new Map(set)
//或者其它map
const map3 = new Map(map);
///方法:
map.set(key, value);// 添加
map.clear();// 清空
map.delete(key);// 删除指定元素
map.has(key); // 判断是否存在
map.forEach(function(key,value){})//遍历元素
map.size; // 元素个数。是属性,不是方法
map.values() //获取value的迭代器
map.keys() //获取key的迭代器
map.entries() //获取entry的迭代器
///用法:
for (let key of map.keys()) {
console.log(key);
}
//或:
console.log(...map.values()); //通过扩展运算符进行展开
1.9、class(类)的基本语法
<script>
class User{
constructor(name, age = 20){ // 构造方法
this.name = name; // 添加属性并且赋值
this.age = age;
}
sayHello(){ // 定义方法
return "hello";
}
static isAdult(age){ //静态方法
if(age >= 18){
return "成年人";
}
return "未成年人";
}
}
let user = new User("张三");
//测试
console.log(user); // User {name: "张三", age: 20}
console.log(user.sayHello()); // hello
console.log(User.isAdult(20));// 成年人
</script>
类的继承:
class User{
constructor(name, age = 20){ // 构造方法
this.name = name; // 添加属性并且赋值
this.age = age;
}
sayHello(){
return "hello"; // 定义方法
}
static isAdult(age){ //静态方法
if(age >= 18){
return "成年人";
}
return "未成年人";
}
}
class ZhangSan extends User{
constructor(){
super("张三", 30); //如果父类中的构造方法有参数,那么子类必须通过super调用父类的构造方法
this.address = "上海";//设置子类中的属性,位置必须处于super下面
}
}
//测试
let zs = new ZhangSan();
console.log(zs.name, zs.address);
console.log(zs.sayHello());
console.log(ZhangSan.isAdult(20));
1.10、Generator函数
<script>
function* hello () {
yield "hello";
yield "world";
return "done";
}
let h = hello();
console.log(h.next()); //{value: "hello", done: false}
console.log(h.next()); //{value: "world", done: false}
console.log(h.next()); //{value: "done", done: true}
console.log(h.next()); //{value: undefined, done: true}
</script>
function* hello () {
yield "hello";
yield "world";
return "done";
}
let h = hello();
for (let obj of h) {
console.log(obj);
}