jquery is shaped like $ ("#txtName"). AddClass ("Err"). CSS ("Font-size", "12px"). Select (). focus (); the way people have to be fascinated. Its implementation mechanism is chained call. A chained call is a method of calling an object to return to the object, strictly speaking it does not belong to the syntax, but just a grammar technique, JS fascinating point is here.
A method that does not return a value belongs to an assignment method, and it is obvious that it is easy to implement a chained call, provided that the use of this pointer is correctly understood.
Copy Code code as follows:
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 ();
<textarea id="runcode8892"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>javascript chained call http://www.jb51.net/</title> <script type=" text/ JavaScript "> 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 (); </script> </pead> <body> </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]