function Dog(name,color){
this.name=name||"";
this.color=color||"";
};
//每个方法返回 this ,以便实现链式调用
Dog.prototype.setName=function(name){
this.name=name;
return this;
};
Dog.prototype.setColor=function(color){
this.color=color;
return this;
};
Dog.prototype.yelp=function(){
alert("我的名字叫:"+this.name+",我的颜色是:"+this.color);
return this;
};
Dog.prototype.getName=function(callback){
callback.call(this);//核心处
return this;
}
//链式调用
new Dog().setName("旺财1").setColor("白色").yelp();
//回调机制
new Dog().setName("旺财2").setColor("白色").getName(function(){
this.color="红色";
this.yelp();
});
js实现链式调用及回调
最新推荐文章于 2023-05-30 11:01:05 发布