A method for writing JavaScript script libraries _javascript tips

Source: Internet
Author: User
Tags pow web services

JavaScript is the so-called client-side scripting language, a computer programming language that runs inside an internet browser (also known as a Web client because it connects to a Web server to download pages). The way JavaScript works is interesting. Some JavaScript code will be inserted into the normal Web page. When the browser loads the page, the browser's built-in interpreter reads and runs the JavaScript code it finds on the page.

Web development has been done for four years, and more or less has accumulated some JavaScript scripts. For example, restricting input allows only a number of scripts to be entered, and a hit return automatically moves to the next control, which is the same as the TAB key; Because the results of JavaScript numeric operations are often not what we want, we have to have floating-point arithmetic (subtraction) functions. Every time you have JavaScript requirements, you often find the required scripts on the Web, copy them directly to the ASPX file, or create a new JavaScript file, and then add a reference
<script src= "Jscript.js" type= "Text/javascript" ></script>, in this way to complete the production of client script. After all, the need for JavaScript is not much, and there is no great effort to learn.

Recently the company's project is not busy, free time to quickly cram the knowledge of the script. There is a popular jquery script library on the web, and there are plenty of articles in the garden to discuss how to use it. In my own experience, JavaScript, like regular expressions, often learns to forget. The knowledge you have learned is not necessary and will soon be forgotten. In particular, and application-related content, such as pagemethods How to use, customer short callback How to implement, how to use JavaScript to invoke Web services, in the project used many times, but a colleague asked up, or vague, it is hard to say why. I have a way is to make a demo, the various effects of the demo well, grouped together, and then use the time to search, which can save a lot of time. Another way to do this is today's article, which is to sort out the JavaScript, and make it a more Generic script library for easy reuse. The meaning of collation is that the function of the appropriate adjustment, so that it can not only meet the needs of the current project, but also to meet the needs of future projects, another meaning is to standardize the naming and organization structure, write good sample code, use it when convenient. Sometimes downloading a lot of JavaScript utility scripts online, but forget to download its test script, do not know how to use, it is not as convenient to search the Internet.

JavaScript is defined as an object-based scripting language, on the one hand, based on the DOM object model and the methods in the DOM object, on the other hand, it does not have the inheritance of object-oriented language, polymorphism characteristics. asp.net Ajax extends JavaScript so that we can organize JavaScript scripts in an object-oriented way. My main job here is to encapsulate and encapsulate the existing code to facilitate the next reuse. As a result, there are two ways to organize an existing JavaScript code base.
I take the addition and subtraction of the controversial floating-point operations in JavaScript as examples to see how to encapsulate them

JavaScript style

function Math () {}//addition Math.prototype.add=function (ARG1,ARG2) {var r1,r2,m; Try{r1=arg1.tostring (). Split (".") [1].length}catch (e) {r1=0} try{r2=arg2.tostring (). Split (".") [1].length}catch (e) {r2=0} m=math.pow (10,math.max (R1,R2)) return (arg1*m+arg2*m)/m}//Subtraction math.prototype.subtraction= 
  function (arg1,arg2) {var r1,r2,m,n; Try{r1=arg1.tostring (). Split (".") [1].length}catch (e) {r1=0} try{r2=arg2.tostring (). Split (".") 
  [1].length}catch (e) {r2=0} m=math.pow (10,math.max (R1,R2)); 
  N= (R1&GT;=R2) r1:r2; 
Return ((arg1*m-arg2*m)/m). ToFixed (n); 
Call method var math=new math (); var result=math. 
ADD (2.0,4.0); 
Ajax style Type.registerNamespace ("Utility"); 
 Utility.math=function (Larg,rarg) {this._left=larg; 
This._right=rarg; 
} utility.math.prototype= {//addition function add:function () {var r1,r2,m; Try{r1=left.tostring (). Split (".") [1].length}catch (e) {r1=0} try{r2=right.tostring (). Split (".") 
[1].length}catch (e) {r2=0} m=math.pow (10,math.max (R1,R2)) return (left*m+right*m)/m}Subtraction function subtraction:function () {var r1,r2,m,n; Try{r1=left.tostring (). Split (".") [1].length}catch (e) {r1=0} try{r2=right.tostring (). Split (".") 
  [1].length}catch (e) {r2=0} m=math.pow (10,math.max (R1,R2)); 
  N= (R1&GT;=R2) r1:r2; 
Return ((left*m-right*m)/m). ToFixed (n); 
}//Registration Class Utility.Math.registerClass ("Utility.math"); 
Then, where needed, the following method can be used to invoke Var math=new utility.math (2.0,4.2); var result=math.  ADD ();

Using the two methods presented above, you can easily encapsulate commonly used javascript and reduce duplication.

There is a problem with the naming above, because math is a type built into JavaScript to handle a variety of mathematical operations, and for the above JavaScript-style script to run, the class name must be replaced with a different name, such as Mathhelper. asp.net Ajax also extends the six types of JavaScript, namely Array,boolean,date,error,object and string.

There are, presumably, friends who will say that you should publish your own JavaScript library so that you can satisfy the public taste. Only this method, to practice it is still very difficult: there is a reason for the project time is tight, every day busy doing projects, where there is time to organize this, there are not familiar with the reasons for JavaScript.

To keep my class library from being a vegetable, I went online to find some advice on how to write a good JavaScript library.

There is an article called "Building a JavaScript Library", originally wrote this article would like to see how he wrote, but the page has been prompted to load the file, cannot view. Would like to know how the foreigner for the same theme, he is how to write.

There is also an excellent article, the name is "rules for JavaScript Library Authors", address in
http://dean.edwards.name/weblog/2007/03/rules/

I translated it for your reference.

1 use method not too cumbersome.
2 Avoid using Object.prototype
3 Do not overdo the expansion
4 comply with the standard.
5 to the best JavaScript creator
6 Keep flexible and variable 7 manage memory and avoid memory leaks.
8 avoid browser-related hack
9 Keep the class library simple
10 Keep class libraries predictable. For example, if you don't see a document, you should be able to guess that math is about dealing with mathematical operations.
11 Bonus rules: document; Use namespaces to organize your code as much as possible, making it easy to remember;

My level is very general, very ordinary a programmer. So don't ask me for code. I gave it to you, you still have to spend time to see it, and my code does not have documents, you can not read how to do it. Instead, why don't you tidy up the javascript you have, and the JavaScript in your hand is actually used by you, and you know him well. And don't recommend jquery, it's not my purpose.
My goal is to teach you how to organize your existing JavaScript script libraries and use the resources you already have on hand.

Test code Download: Http://xiazai.jb51.net/201509/yuanma/Math-Test (jb51.net). rar

Add a common problem: if you put JavaScript in an external file, the runtime might prompt "Object not found"
This problem is caused by file encoding. To keep the encoding of JavaScript script files consistent with the file encoding of HTML pages
Click File--> Save As option to store both in the same encoding format

It is recommended that you script with the VS IDE so that you can use the smart hints provided by the IDE to support

If you write a script with Dreamweaver, it also provides a smart hint


The above content for you to introduce the JavaScript script library writing methods, I hope you like.

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.