return this 的用法

function zz(){
  return {
    a: function (){
      console.log(1);
      return this;
    },
    b: function (){
      console.log(2);
    }
  }
}this

var h = new zz()spa

h.a().b()  //  1  2
h.b().a() //  2   Uncaught TypeError: Cannot read property 'a' of undefined對象


return this    this是指向當前對象的引用,return this就是把這個引用返回。能夠返回對象自己這樣子能夠進行鍊形調用

io

相關文章
相關標籤/搜索