JavaScript series: Modular and chained programming

Source: Internet
Author: User

modularity : Closures and function scopes (JS without block-level scope ES6) Construction module

1 varman=function(){2             varAge=12;3             return {4GetYear:function(){5                     returnAge ;6                 },7Setyear:function(n) {8Age=N;9                 }Ten             } One         } A  -         varPeter=Man (); -document.write (Peter.getyear ());// A thePeter.setyear (20); -document.write (Peter.getyear ());// - -document.write (Peter.age);//undefined property Private can only be accessed through the interface
no new, create object. Restrict access to the properties of this object (only by means of this object)

1, no new, create object.

2, restrict the access to this object's properties (this object's properties can only be obtained by means of this object )

Chained programming:

1 varman=function(){2             varAge=12;3             return {4GetYear:function(){5 alert (age);6                     return  This;7                 },8Setyear:function(n) {9Age=N;Ten                     return  This; One                 } A             } -         } -  the         varPeter=Man (); -Peter.getyear (). Setyear ((). GetYear ();//Cascade
1 No new, create object. 2 Each method, except the action, will return this,this to this {} Nameless JSON object. There are both actions and return objects. Reach Chain Type

1, no new, create object.

2, each method, except the action, will return This,this point to this {} Nameless JSON object.

both actions and objects are returned . reach chain Type

JavaScript series: Modular and chained programming

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.