Introduction to the simulation of object-oriented technology in JavaScript

Source: Internet
Author: User

I. Introduction
In C # and Java languages, object-oriented is implemented in the form of classes, especially by inheriting this feature. The Inheritance of classes shows powerful functions and is easy to learn. JavaScript is not a pure object-oriented language, but an object-based language. The Inheritance of objects is inherited in the form of prototype functions, which many beginners do not understand at the beginning, however, it is not only feasible to implement object-oriented technology in the form of prototype functions, but also provides dynamic inheritance functions for object-oriented technology, this article mainly discusses the object-oriented technology of JavaScript.
Ii. Overview of prototype objects
Each JavaScript Object has an original type object, which inherits all attributes of the prototype object. The prototype of an object is defined by the constructor that creates the object. All functions in JavaScript have a property named prototype, which references the prototype object. When the prototype object is initialized, only the constructor property references the object for creating the prototype object. JavaScript does not have the Class definition Class concept. The constructor defines the Class and initializes the attributes in the Class. Each Class member will inherit the same attributes from the prototype object, that is, the prototype object provides attributes and Methods shared by class instances, which saves memory.
When reading an object's attribute, JavaScript will first find it from the object. If not, it will find this attribute (or method) in the prototype object. Therefore, especially for methods, it is best to save them to the prototype object to facilitate sharing and save memory. In addition, the prototype object has a powerful function, that is, if some objects are instantiated by the constructor and attributes and methods are added to the prototype object of the constructor, the original instantiated object instance will inherit these added attributes and methods.
3. Object Attributes, object methods, class attributes, and class methods
Each object has its own unique instance attribute and copy of the instance method. If five objects are instantiated, there will be five instance attributes and instance method copies of the five objects. This keyword references their instance objects. That is to say, whoever operates the instance method, this will reference the instance object. this will reference the attributes of the accessed instance object.
Class methods and class attributes have only one copy. Class names must be referenced when class methods are called, for example, Date. setHours ();
The following uses a program to display instance attributes, instance methods, class attributes, and class methods.
Copy codeThe Code is as follows:
Function Mobile (kind, brand ){
This. kind = kind; // define the type of mobile phone, such as GSM/CDMA
This. brand = brand; // defines the brand of the mobile phone. this keyword indicates the object after the constructor is instantiated.
}
  
/**//*
The second step of defining a class is to define its instance method or other attributes in the prototype object of the constructor.
Any attribute defined by this object inherits all instances of this class.

*/
// Dialing. Only the phone number is returned.
Mobile. prototype. dial = function (phoneNo ){
Return phoneNo;
};
  
  
/**//*
The third step of the definition class is to define class methods, constants, and other necessary class attributes as the attributes of the constructor, rather than the constructor.
Property of the prototype object. Note that the class method does not use the keyword this because they only operate on their actual parameters.
*/
// Shutdown upon startup
Mobile. turnOn = function (){
Return "The power of mobile is on ";
}
Mobile. turnOff = function (){
Return "The power of mobile is off ";
}
  
  
// Class attributes so that they can be used as constants. Note that they are not read-only.
Mobile. screenColor = 64 K; // assume that the screen color of this type of Mobile phone is 64 K.
Iv. Subclass
JavaScript supports subclass. You only need to instantiate the prototype object of the subclass with the superclass. However, you should note that there will be a problem after subclass, because the prototype object of the subclass is instantiated with a superclass, the constructor attribute provided by JavaScript is washed out. To ensure the correctness of the constructor, You need to specify it again, an example of a subclass program is as follows:
/***** Subclass *****/
// Below is the subclass constructor smart phone
Function SmartPhone (OS)
{
This. OS = OS;

}
// We use the Mobile object as its prototype
// This means that the instance of the new class will inherit SmartPhone. prototype,
// The latter is inherited by Mobile. prototype.
// Mobile. prototype is inherited by Object. prototype
SmartPhone. prototype = new Mobile (GSM, Nokia );
// Add a new method to the subclass to send an Email. Here, only the Email address is returned.
SmartPhone. prototype. sendEmail = function (emailAddress ){
Return this. emailAddress
}
// The subclass method above has some drawbacks. Because we explicitly set SmartPhone. prototype to an object we have created, it overwrites
// Prototype object, and the given Constructor attribute is discarded. This property references the constructor that creates this object. However, the SmartPhone object integrates its
// Constructor of the parent class, which does not have this attribute. Setting a property can solve this problem:
SmartPhone. prototype. constructor = SmartPhone;
Var objSmartPhone = new SmartPhone (); // instantiate a subclass

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.