Javascript basics and object-oriented Basics ~ 8. From the origin of human beings. Object-oriented 4. class objects in Javascript

Source: Internet
Author: User
ArticleDirectory
    • JS encapsulation:
    • Inheritance in JS:
    • Classes in JS

Back to directory

I am honored to write this ending. Sometimes, we need to look at people, things, and things with care, rather than eyes, because what we see with eyes is often a false image of things, i'm sorry to say this to my most lovely brother.

From the origin of human beings, we talk about object-oriented. In fact, we mainly talk about object-oriented. Now, let me briefly talk about object-oriented features.ProgramAfter being written as object-oriented, it has three advantages:

Encapsulation, inheritance, and polymorphism are common characteristics of all object-oriented languages. For JavaScript, you can also use them for reference, such as JS encapsulation and JS inheritance.

JS encapsulation:

When we do something, we needCodeThis process is called encapsulation. When using this function, you only need to sign the function method.

The implementation details need to be taken into account. The Code is as follows:

 //  Verification Form  Function chkformfun (name, password, email ){  If (Name =''  ) {Alert (  '  The user name cannot be blank.  '  );  Return   False  ;}  If (Password = ''  ) {Alert (  '  The password cannot be blank.  '  );  Return  False  ;}  If (Email = ''  ) {Alert (  '  Email cannot be blank  '  );  Return   False  ;}  Return   True  ;} 

We need to directly call where form verification is requiredChkformfunThis method does not need to know its implementation details.

Inheritance in JS:

In part, I have a detailed introduction to class objects in Javascript. You can check them, the main idea of inheritance is that it will extract the common attributes and methods of a class of objects to a new object.

This object is called a base class. In fact, the related object can inherit it, so that the information in the base class fully inherits the quilt class, And the subclass can be based on its own needs, override the attributes and methods of the base class.

Because JS does not provide an Object Inheritance Mechanism, we need to use some JS techniques to achieve this effect. Its principles are as follows:

1. Call the parent class Constructor (parent class. Call () in the constructor of the subclass ()).

2. Modify the prototype attribute of the subclass to the instance of the parent class. (prototype is generally used to extend the function. Dog. Prototype =NewAnimalfun ();Indicates parent classAnimalfun)

3. Reset the constructor attribute of prototype to subclass.

Classes in JS

The following defines a human-related code, which reflects the class in JS, the attributes in the class, and how the class behavior is implemented.

VaRPerson = {//Target personName:'Zzl',//AttributeFacial: ['eye ', 'nose', 'mouth'],//AttributeWork: function (){//MethodConsole. Log (This. Name +'This person started to work.');}}

When you call this human object, you can:

Console. Log ("Person's name:"+Person. Name); console. Log ("Human facial features:"+Person. Facial); person. Work ();//People are about to go to work

Finally, I wish all the good people a good friend and a good friend. Finally, I would like to thank God for giving me the best friend. Thank you!

Thank you for reading!

Back to directory

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.