Fork me on GitHub

mark jquery 链式调用的js原理

  我们在使用jquery的时候会用到类似$("#id").css('color','red').show(200);

     这样写有点减少代码量,减少了逐步查询DOM的性能损耗;

     js 原理实现:

    

function demo(){}

demo.prototype={
     first:function(fir){
      this.fir=fir;
       return this;},
    second:funciton(sec){
       this.sec=sec;
       return sec;
    }
}
var s=new demo();
s.first("fir").second("sec");
alert(JSON.stringify(s));{'fir':'fir','sec':'sec'}

  这里由于return this ,所以链式调用类似demo.first("fir")=this;  demo.first("fir").second("sec")=this.second("sec")=demo.second("sec");

     

posted @ 2015-03-24 21:34  Kingler  阅读(335)  评论(0编辑  收藏  举报