Javascript uses function to create classes in two ways

Source: Internet
Author: User
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.

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.