2021-07-23

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);
  1. Array.prototype.slice.call()
var obj = {
    0:"张三",
    1:"李四",
    length:2
}
var brr = Array.prototype.slice.call(obj)
console.log(brr);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文链接:MySQL索引背后的数据结构及算法原理[http://blog.codinglabs.org/artic...
    aa043fefccbf阅读 573评论 0 0
  • 闭包 变量作用域 变量根据作用域不同可以将函数分为全局变量和局部变量 函数内部可以使用全局变量 函数外部不可以使用...
    alfalfaw阅读 224评论 0 0
  • 1:es6中没有变量提升,所以必须先定义类,才能通过类实例化对象 2:类里面的共有属性和方法一定要加this使用 ...
    coder军阅读 337评论 0 0
  • 1.js当中有哪些数据类型 5个基础:字符串,布尔,数值,null,undefined,1个复杂:Object在e...
    林不羁吖阅读 266评论 0 0
  • 今天青石的票圈出镜率最高的,莫过于张艺谋的新片终于定档了。 一张满溢着水墨风的海报一次次的出现在票圈里,也就是老谋...
    青石电影阅读 10,368评论 1 2