How JavaScript classes are written

Source: Internet
Author: User

In JavaScript, a function is a class, which is used within a function to set public member variables and methods for a class, for example:

[JavaScript] View plaincopy

  1. function MyClass (name) {
  2.     var str = "private string"; //private Field
  3.     function Privatefn () { //private method alert (str);
  4. };
  5.     THIS.name = name;
  6.     THIS.PUBFN = function () {
  7. Privatefn (); //call Private Method
  8. Alert ("MYCLASS.PUBFN");
  9. }
  10. }

Create an object with new when used:

[JavaScript]View Plaincopy
    1. var obj = new MyClass ("123");
    2. var name = Obj.name; //access public Field
    3. OBJ.PUBFN (); //call Public Method

The above is the most common way of writing JavaScript class, the actual application is less, the most used is written in the form of static class, JavaScript static class has two ways to write:

1. Writing a JSON-like object

[JavaScript]View Plaincopy
    1. var MyClass = {
    2. Name: "123",
    3. PUBFN: function () {
    4. Alert ("pubfn1 is called");
    5. }
    6. };

Call form: Myclass.name, MYCLASS.PUBFN ()

This wording makes the members of the class access public.

2. How anonymous functions are written

[JavaScript]View Plaincopy
  1. var MyClass = function () {
  2.     var name = "123"; //private Field
  3.     var privatefn = function () { //private method
  4. alert (name);
  5. }
  6.     return{ //return public field and method
  7. Name: "Hello" + name, //public field
  8. PUBFN: function () {
  9. Privatefn (); //call Private Method
  10. Alert ("This is a returned function"); //public Method
  11. }
  12. }
  13. }();

The advantage of this notation is that private and common member methods and variables can be separated, and in practice the complex logic can be written in private methods, and return a common interface calls the private method.

Call form: MyClass. Name, MYCLASS.PUBFN ()

Note the parentheses at the end of the class, which is the function of creating the object and removing the parentheses, then the call form is: MyClass (). Name, MyClass (). PUBFN ().

How JavaScript classes are written

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.