目录
定义变量
var 声明变量是在全局范围内有效,可以重复使用,存在变量提升
let 不可以重复声明,块级作用域
const 声明一个只读的常量,一旦声明,常量的值就不能改变
展开数组
相当将数组的内容拿出来 例子:arr=[0,1,2] 就相当于 arr=0,1,2 也可以当做实参传入函数
function myFunction(x, y, z) { }
let args = [0, 1, 2];
myFunction(...args);
默认参数
function(a=5,b=7){}
5和7就相当于给了一个默认参数 ,但传入是参数时,使用传入的值,如果不传,使用默认的参数
面向对象
class关键字,构造器和类分开了
class里面直接加方法
class User{
constructor(name,age){ //constructor构造器
this.name=name;
this.age=age;
}
eat(){//实例的方法
alert('eating');
}
}
继承
class Vipuser extends User{ //extends必须要有User要继承的对象
constructor(name,age){
super(name,age);//继承user的那几个属性
this.run(); //继承父类的方法
}
}
解构赋值
左右两边的结构必须一样,右边的写法必须规范,声明和赋值不能分开
let a,b,c
[a, b,c] = [10, 20,30]
str.includes()
判断字符串中是否含有某些字符
console.log('abc'.includes('a')); //true
trimStart()和trimEnd()基本用法
清除字符串的首或尾空格,中间的空格不会清除
const s = ' a b c ';
console.log(s.trimStart()); //a b c
console.log(s.trimEnd()); // a b c
Array.from()方法
将其他数据类型转化为数组
console.log(Array.from('str')); //(3) ["s", "t", "r"]
Map对象
Map
对象保存键值对,并且能够记住键的原始插入顺序
let myMap = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
])
map.set(),map.get()
const map1 = new Map();
map1.set('1', 'one');
console.log(map1.get('1'));//"one"
Set 对象
Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用
set.has()
const set1 = new Set([1, 2, 3, 4, 5]);
console.log(set1.has(1));//true
set.add()
const set1 = new Set();//set对象的值都是不相同的
set1.add(42);
set1.add(42);
set1.add(13);
for (const item of set1) {
console.log(item);//42,13
}
Es7新特性
Array.prototype.includes() 方法
在 ES6 中我们有 String.prototype.includes() 可以查询给定字符串是否包含一个字符,而在 ES7 中,我们在数组中也可以用 Array.prototype.includes 方法来判断一个数组是否包含一个指定的值,如果包含则返回 true,否则返回 false
const arr = [1, 3, 5, 2, '8', NaN, -0]
arr.includes(1) // true
求幂运算符 **
在 ES7 中引入了指数运算符,具有与 Math.pow() 等效的计算结果
console.log(2**10);// 输出 1024
console.log(Math.pow(2, 10)) // 输出 1024
Es5新特性
ES5的严格模式 ‘use strict’
给作者提供了选择一个限制性更强语言变种的方式
1. 消除不安全之处,保证安全运行
2. 提升编译效率
3. 未来发展趋势
规则:
1)变量先定义在使用
2)不允许变量重名
var a=1;
function a(){}
3)不允许使用eval
4)不允许delete
4)不允许with语句
var sMessage = "hello";
with(sMessage) {alert(toUpperCase());}
新增方法
// 访问原型
Object.getPrototypeOf(object)
// 阻止向对象添加属性
Object.preventExtensions(object)
// 防止对对象进行任何更改
Object.freeze(object)
Array.prototype.indexOf
//跟字符串查找一样,查找指定元素是否存在,如果存在,返回下标,如果不存在返回-1
let arr = [1,2,3,4];
console.log(arr.indexOf(3)); // 2
Array.prototype.forEach
arr.forEach(function(value, index, array) {
//参数一是:数组元素
//参数二是:数组元素的索引
//参数三是:当前的数组
})
//相当于数组遍历的 for循环 没有返回值
Function.prototype.bind
bind会返回一个函数,不会立即执行
call 立即执行 (window,1,2,3)
apply 立即执行 (window,[1,2,3])