This year contacted a B/S project, summed up some JavaScript development experience, organize some useful content to share with you. This article will continue to update ...
1 . Implement code access control
As the project JavaScript code base expands, the functions and variables that should be controlled in the private scope will leak more and more, and your global scope will become more and more likely to be contaminated. This not only causes naming conflicts, but also provides a hotbed for bugs. Therefore, we need to write a self-contained, opaque JavaScript code that does not leak internal details to the outside world and does not conflict with existing frameworks and script libraries. In fact, this is the main requirement for large-scale programming
Examples are as follows:
//to define an obj object varobj = (function () { varPrivatefield = 42; varPublicfield = ' Foobar '; //obj Object Private Method functionprocessinternals () {alert (' Private: ' +Privatefield); } //obj Object Private Method functionrun () {processinternals (); Alert (' Private: ' +Privatefield); Alert (' Public: ' +Publicfield); } return{Publicfield:publicfield,//returns the name of the Publicfield private variableRun:run//returns the name of the run private method }; })();//Note the increase () here;Obj.run ();//call the Obj object Run () methodObj.publicfield;//Call obj Object Public Propertyobj.processinternals ();//Access Private method, nullObj.privatefield;//Access private property, null
2. use optional/variable/named parameters
JavaScript Development Experience Finishing