_javascript tips for chained calls in JavaScript

Source: Internet
Author: User
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]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.