Javascript uses function to create classes in two ways. 1. Use function classes.
// MyFunction. jsvar CMyFunc = function () {// public method of the class for external calls this. func1 = function () {var I = 0; return I;} this. func2 = function () {_ privateFunc () ;}// private method in the class for the public method to call function _ privateFunc () {return 0;]} CMyFunc myFunc = new CMyFunc ();
Usage: After myFunction. js is introduced to other javascript files, myFunc (global variable). Func1 is directly used.
2. Use function class (2)
// MyFunction. jsvar CMyFunc = function () {var myFunc = new Object (); // public method of the class for external calls of myFunc. func1 = function () {var I = 0; return I;} myFunc. func2 = function () {_ privateFunc () ;}// private method in the class for the public method to call function _ privateFunc () {return 0;] return myFunc ;}
Use: After myFunction. js is introduced to other javascript files, use var myFunc = new CMyFunc () to initialize an object using
Advantages: 1. There is a smart prompt when using the myFunc object function. 2. You do not need to create a full variable in myFunction. js. 3. The this parameter in the myFunc object function points to the same
3. Use jquery Extension
//myFunction.js(function ($) {$.MyFunc={}; $.MyFunc.Func1=function(){var i=0;return i;} $.MyFunc.Func2=function(){var i=0;return i;} })(jQuery);
Use: After myFunction. js is introduced to other javascript files, use $. MyFunc. Func1 () directly.