面向面試之 JavaScript 篇(一)

拾夕發表於2019-04-17

繼承

extends 實現

class Animal{
  constructor(color){
    this.color = color
  }
  eat(){console.log( 'I can eat')}
}

class Pig extends Animal{
  constructor(name,color){
    super(color)
    this.name = name
  }
  run(){ console.log( 'I can run')}
}

var pig  = new Pig('xiaozhu','red') 
複製程式碼

class 繼承

ES5 實現

function Animal(color){
  this.color = color
}

Animal.prototype.eat = function(){
    consoel.log('I can eat')
  }

function Pig(name,color){
  this.name = name
  Animal.call(this, color)
   
}
Pig.prototype.constructor = Pig
function fn(){}
fn.prototypr = Animal.prototype
Pig.prototype = new Animal()
Pig.prototype.run = function(){
    console.log('I can run')
  } 
var pig = new Pig('peiqi','red')

console.log(pig)
pig.run()
複製程式碼

ES 5 繼承

手寫 AJAX

手寫 AJAX

防抖 節流

防抖

節流

Promise

Promise、Promise.all、Promise.race

  1. Promise 用法
 function fn(){
    return new Promise((resolve, reject)=>{
        成功時呼叫 resolve(資料)
        失敗時呼叫 reject(錯誤)
    })
 }
 fn().then(success, fail).then(success2, fail2)
複製程式碼
  1. Promise.all 用法
 Promise.all([promise1, promise2]).then(success1, fail1)
複製程式碼

promise1和promise2都成功才會呼叫success1

  1. Promise.race 用法
  • Promise.race([promise1, promise2]).then(success1, fail1)

  • promise1和promise2只要有一個成功就會呼叫success1

相關文章