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

相关文章
相关标签/搜索