欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

es6零碎知识点整理

程序员文章站 2022-07-16 20:15:00
...

module

  • components/HelloWorld.vue
import { a } from "@/modules";
import b from "@/modules";
export default {
  mounted() {
    console.log(a);
    b();
  }
};
复制代码
  • modules/index.js
let a = 'JonSnow'
let b = 'Cercei'

export {
    a
}

export default () => console.log(b);
复制代码

  • components/HelloWorld.vue
import b from "@/module";
export default {
  mounted() {
    b().then(res => {
      console.log(res);
    });
  }
};
复制代码
  • modules/index.js
export default () => new Promise((resolve, reject) => {
    setTimeout(() => {
    	resolve('es6 module')
    }, 200)
})
复制代码

class

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  say() {
    console.log(`${this.name}'s age is ${this.age}`);
  }
}
class Student extends Person {
  constructor(name, age, sex) {
    super(name, age);
    this.sex = sex;
  }
  saySex() {
    console.log(
      `${this.name}'s age is ${this.age} and his sex is ${this.sex}`
    );
  }
}
let JonSnow = new Student("JonSnow", 21, "男");
JonSnow.say();
JonSnow.saySex();
console.log(JonSnow);
console.log(JonSnow instanceof Student);
console.log(JonSnow instanceof Person);
复制代码

includes && indexOf

let arr = [1, 2, 3];
console.log(arr.includes(1));
console.log(arr.indexOf(100));
复制代码

全局作用域与函数作用域

var a = 12;
function fn() {
  console.log(a);
  var b = 13;
  if (true) {
    var c = 14;
    console.log(b);
  }
  console.log(c);
}
fn();
console.log(c);//报错
复制代码

函数执行完后,变量就被销毁了

使用undefined传参

function myFunction(x = 1, y = 2, z = 3) {
  console.log(x, y, z);//1,7,9
}
myFunction(undefined, 7, 9);
复制代码

参数运算

function myFunction(x, y, z = x + y) {
  console.log(x, y, z);//1,2,3
}
fn(1, 2);
复制代码

push

let a1 = [1];
let a2 = [2, 3, 4];
// a1.push(2, 3, 4);
a1.push(...a2);
console.log(a1);
复制代码

忽略数组中的某些值

let [a, , b] = [1, 2, 3];
console.log(a); //1
console.log(b); //3
复制代码

使用展开语法

let [a, ...b] = [1, 2, 3];
console.log(a); // 1
console.log(b); //[2,3]
复制代码

let [a, , ...b] = [1, 2, 3, 4, 5];
console.log(a); //1
console.log(b); //[3,4,5]
复制代码

let [a, b, c = 3] = [1, 2];
console.log(a); //1
console.log(b); //2
console.log(c); //3
复制代码

function myFunction([a, b, c = 3] = [1, 2, 3]) {
  console.log(a, b, c);//1 2 3
}
myFunction();
复制代码

function myFunction([a, b, c = 3] = [1, 2, 3]) {
  console.log(a, b, c);//1 2 3
}
myFunction(undefined);
复制代码

转载于:https://juejin.im/post/5ce4b256f265da1bb80c0402