javascript中的链式调用_javascript技巧
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript中的链式调用_javascript技巧,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1177字,纯文字阅读大概需要2分钟。
内容图文
![javascript中的链式调用_javascript技巧](/upload/InfoBanner/zyjiaocheng/311/0279913cf8d44ae694db78f7733f2967.jpg)
没有返回值的方法属于赋值器方法,显然它很容易实现链式调用,前提是正确理解this指针的用法。
代码如下:
function W(){
this.name="Wang Hongjian";
this.gender="male";
}
W.prototype.sayHi=function(){
alert("Hello,everybody\nMy name is " + this.name);
return this;
};
W.prototype.doSomething=function(){
alert("I'm working");
return this;
}
W.prototype.sayGoodbye=function(){
alert("Goodbye,everybody");
return this;
};
var w=new W();
w.sayHi().doSomething().sayGoodbye();