Programmer Prerequisites-Create JS Library

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span><span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Beef Brisket The JavaScript method that is explained in JavaScript is built on the HTML page, on the one hand exposes its own method implementation process, second, if other pages need the same method, it will have to write the same method again, Can not be achieved very good reuse. Build JS library, through the script tag <script></script> src attribute Introduction, JS method can be arbitrarily called. Not only does the implementation method not be exposed in the HTML page, And let the function of the use of greatly improved. On the other hand, this is the experience of the programmer accumulated savings, can put some of the functions in the future, when needed, to use .</span>

Next start to write your own JS library, first prepare the template:

<span style= "FONT-SIZE:14PX;" > (function () {window[' namespace ']={}window[' namespace '] [function ']=$;}) ();</span>

Note here, the composition of the symbol must not be missing, if you have the source template, to try to paste the copy, reduce the errors caused by handwriting.

Use this template to create a function:

<span style= "FONT-SIZE:14PX;"              > (function () {//Register namespace IC with window.                  window[' JG ']={} function $ () {//definition element array, array object.                  var elements=new Array (); Loop validation of an array.                  Arguments is an internal object returned by JS, returning the function passed by the parameter object, in the form of an array.                      for (Var i=0;i<arguments.length;i++) {//Define element object, read the parameter array.                      var element=arguments[i];                      Determines whether the original element in the array is a string type.                      if (typeof element== ' string ') {Element=document.getelementbyid (element);                      }//To determine whether the current parameter knows only one, we will return this parameter directly.                      if (arguments.length==1) {return element; }//If more than one parameter is said.                      We put in the array elements.                  Elements.push (Element); }                      //Take all the whole out and return.              return elements;          }//Register the namespace JG on Windows and register the $ method with the namespace.   window[' JG ' [' $ ']=$; }) ();</span>
in the Script tab, refer to the JS Library:

<script type= "Text/javascript" src= "Cjg.js" ></script> ps:src= "JS file path"


Next, the method in JS can be called, invoking an instance:

<span style= "FONT-SIZE:14PX;" ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">




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.