es6
- 数组新增方法
some - 判断数组中是否至少有一个元素是满足指定条件的
var arr = [2,4,6,8,10];
var bool = arr.some(function(v){
return v<5
})
console.log(bool);
every - 判断数组中是否每个元素都满足指定的条件
var arr = [2,4,6,8,10];
var bool = arr.every(function(v){
return v>1
})
console.log(bool);
find - 查找满足指定条件的第一个元素
var arr = [2,4,6,8,10];
var ele = arr.find(function(v){
return v>5
})
console.log(ele);
findIndex - 查找满足条件的第一个元素的下标
var arr = [2,4,6,8,10];
var index = arr.findindex(function(v){
return v>5
})
console.log(index);
- 字符串
模板字符串
var str = `
a
b
c
d
`
console.log(str);
startsWith - 判断字符串是否以指定的字符开头
var str = 'abcdef';
var bool = str.startsWith('ab')
console.log(bool);
endsWith - 判断字符串是否以某个字符结尾的
var str = 'abcdef';
var bool = str.endsWith('e')
console.log(bool);
includes - 判断字符串中是否包含某个字符
var str = 'abcdef';
var bool = str.includes('bcd')
console.log(bool);
- 变量定义
let
let a = 10;
console.log(a);
// let定义的变量不能预解析
// console.log(a);
// let a = 10;
// let定义的变量不在window上
// let a = 10;
// console.log(window);
// let定义的变量不能重复定义
let a = 10;
let a = 20;
console.log(a);
// let会自带一个作用域,将作用域限制在大括号中
if(true){
let a = 30
}
console.log(a);
// 块级作用域
const 定义常量
const的特性跟let一样,const的值不能改
- 箭头函数
var fn = ()=>{
console.log("这是fn函数");
}
fn()
- 对象
解构赋值
var obj = {
username:"张三",
age:22
}
var {username,age} = obj;
// 解构起别名
// var {username:u,age:a} = obj;
console.log(username,age)
数组解构
var arr = [1,2,3,4,5];
var [_,b] = arr;
console.log(b);
// 不利用第三个变量交换两个变量的值
var a = 1;
var b = 2;
var [b,a] = [a,b]
console.log(a,b);
- ...运算符
...数组
var arr = [1,9,3,7,4,6,2,5,8];
var max = Math.max(...arr)
console.log(max);
其他
- 检测数据类型
Object.prototype.toString.call(数据)
console.log( Object.prototype.toString.call([]) );
console.log( Object.prototype.toString.call('') );
console.log( Object.prototype.toString.call(Math) );
console.log( Object.prototype.toString.call(new Date) );
console.log( Object.prototype.toString.call(/a/) );
- this关键字
全局的this - window
对象方法中的this - 对象
事件函数中的this - 事件源
定时器中的this - window
普通函数中的this - window
自调用函数中的this - window
箭头函数中的this - 箭头函数上一行代码的this - 改this
function fn(a,b){
console.log(a+b);
console.log(this);
}
var obj = {
name:"张三"
}
// call
fn.call(obj,1,2)
// apply
// fn.apply(obj,[1,2])
// bind
// var a = fn.bind(obj)
// console.log(a,fn);
// a(2,3) -obj
// fn(1,2) -window
- 伪数组
伪数组转数组
1.遍历数组
var obj = {
0:"张三",
1:"李四",
length:2
}
var arr = [];
for(var i=0;i<obj.length;i++){
arr.push(obj[i])
}
console.log(arr);
- Array.prototype.slice.call()
var obj = {
0:"张三",
1:"李四",
length:2
}
var brr = Array.prototype.slice.call(obj)
console.log(brr);