码迷,mamicode.com
首页 > Web开发 > 详细

js实现方法的链式调用

时间:2016-12-24 19:49:01      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:class   div   return   lov   status   code   query   span   stat   

假如这里有三个方法:
person.unmerried();
person.process();
person.married();
在jQuery中通常的写法是:person.unmerried().process().married();
而在js中要实现链式调用,只需在类中的每个方法中通过this关键字返回对象实例的引用。

function Person(){};
Person.prototype.status =false;
Person.prototype.married =function(){
this.status = true;
return this;
};
Person.prototype.unmerried = function(){
this.status = false;
return this;
};
Person.prototype.process = function(){
alert("I‘m in love");
return this;
}
var bob = new Person();
bob.unmerried().process().married();

 

js实现方法的链式调用

标签:class   div   return   lov   status   code   query   span   stat   

原文地址:http://www.cnblogs.com/littlewriter/p/6218011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!