Javascript basics and object-oriented Basics ~ Fourth, class objects in Javascript

Source: Internet
Author: User
ArticleDirectory
    • Occupation defines the class:
    • The following table lists the struct types of the occupied parts:
    • Code for implementing struct:
    • Code for implementing complex classes:
    • TIPS: The Inheritance Mechanism of JS implementation classes is as follows:

Back to directory

Today, how to implement classes in JS is actually written last night. But I did not say anything, so I just say "sorry "! The class in JS is the base of JS object-oriented and also the best thing you can do.CodeWhether or not we can raise a level is generally seen from this point.

Occupation defines the class:

A class is an abstraction of things in reality. A class generally consists of attributes and Methods. attributes are fixed information, and methods are some behavior of classes, classes are often used to abstract some complex objects. Classes and classes can be inherited. They are generally implemented through JS functions. class objects in JS are the basis for JS object-oriented implementation.

The following table lists the struct types of the occupied parts:

A struct is also an abstraction of real things. It is used to represent simple logic objects. It is generally implemented through JS object objects.

Code for implementing struct:
//Simple struct objectVaRAnimal ={Name:"Animal", Type:"", Food :["Grass", "meat"], Print:Function() {Console. Log ("Name:" +This. Name + ", type:" +This. Type + ", food:" +This. Food) ;}}; animal. Print ();//Call
Code for implementing complex classes:
 // Complex type objects          VaR Animalfun = Function  (Name, type ){  This . Name = Name;  This . Type = Type;  This . Food = ["Grass", "meat" ];  This . Print = Function  () {Console. Log ( "Name:" +This . Name + ", type:" + This . Type + ", food:" + This  . Food );}};  VaR Animalfun = New  Animalfun (); animalfun. Print (); 

The subclass inherits the parent class, but returns the attributes of the parent class and calls the method of the parent class. The Code is as follows:

 //  Subclass can inherit the parent class and rewrite its attributes.          VaR Dog = Function  (Name, type, food ){ //  Call the parent class constructor and pass the name parameter. Animalfun. Call ( This  , Name, type );  //  Secondary write attribute              This . Food = Food ;}  //  Point dog's prototype chain to animalfun object Dog. Prototype = New  Animalfun ();  // Reset the constructor attribute to the student class. Because the prototype of the student class is set to person          //  Remove the constructor attribute Dog. Prototype. constructor = Dog;  //  Instantiate Dog class          VaR S = New Dog' );  //  Method for calling the parent class S. Print (); //  Result: Name: Dog, type: low-level animal, food: Meat 
TIPS: The Inheritance Mechanism of JS implementation classes is as follows:

Call the parent class Constructor (parent class. Call () in the constructor ()).

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

Resets the constructor attribute of prototype of a subclass to a subclass.

Thank you for reading this article!

Back to directory

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.