es6零碎知识点整理

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值