On simulation of object-oriented technology in JavaScript

Source: Internet
Author: User
Tags add define object constructor copy inheritance instance method return
Object

First, the introduction
In the C # and Java languages, object-oriented is implemented in a class manner, especially inheriting this feature, and the way inheritance of classes shows powerful features and is easy to learn. JavaScript is not a pure object-oriented language, but object-based language, object inheritance is in the form of prototype function, many beginners are not very understanding when they first contact, but JavaScript this form of prototype function to achieve object-oriented technology, not only is feasible, And also provides the dynamic inheritance function for the object-oriented technology, this article mainly discusses the JavaScript object-oriented technology.
II. Overview of Prototype objects
Each JavaScript object has a prototype object that inherits all of the properties of the prototype object. The prototype of an object is defined by the constructor that created the object. All functions of JavaScript have a property named prototype, which refers to a prototype object that, when initialized, has only the constructor property to reference the object that created the prototype object. JavaScript does not have the concept of class-defined classes, the constructor defines the class, and initializes the properties in the class, and the members of each class inherit the same attribute from the prototype object, which means that the prototype object provides the properties and methods shared by the instance of the class, which saves memory.
When reading an object's properties, JavaScript looks for it from the object, and if it is not found, looks for the attribute (or method) in the prototype object, so, especially for the method, it is best to save it to the prototype object for easy sharing and to save memory. And the prototype object has a powerful function, that is, if you instantiate some objects through the constructor, and then add properties and methods to the constructor's prototype object, then the object instance that it originally instantiated will inherit these added properties and methods.
Object properties, Object methods, class attributes, class methods
Each object will have its own copy of instance properties and instance methods, and if 5 objects are instantiated, then there will be 5 instances of objects with instance properties and instance method replicas. This keyword refers to their instance object, that is, who is manipulating the instance method, who is referencing, and which instance object's properties are accessed, this refers to the instance object.
Class methods and class properties have only one copy, and class method calls must refer to the name of the class, for example: Date.sethours ();
The following is a program to represent instance properties, instance methods, class attributes, class methods
function Mobile (Kind,brand) {
this.kind=kind;//defines the type of mobile phone, such as GSM/CDMA
this.brand=brand;//defines the brand of a mobile phone, this keyword represents an object that is instantiated with the constructor
}
  
/**//*
The second step in defining a class is to define its instance method or other properties in the stereotype object of the constructor
Any property defined by the object inherits all instances of that class.
  
*/
dialing, just return the phone number here
Mobile.prototype.dial = function (Phoneno) {
return Phoneno;
};
  
  
/**//*
The third step in defining a class is to define class methods, constants, and other necessary class properties as properties of the constructor itself, not constructors
Properties of the prototype object, note that the class method does not use the keyword this, because they only operate on their actual parameters.
*/
The method of opening an organ machine
Mobile.turnon=function () {
Return "The power of the mobile are on";
}
Mobile.turnoff=function () {
Return ' The power of ' mobile is off ';
}
  
  
Class properties so that they can be used as constants, and note that they are not actually read-only
mobile.screencolor=64k;//assumes that the screen color of this type of cell phone is 64K.
Iv. sub-class
JavaScript supports subclasses, only the primitive objects of the object class can be instantiated with superclass, but it should be noted that this kind of class will have a problem, because the superclass instantiation of a subclass of the prototype object obtained, So we flush out my own constructor attribute provided by JavaScript, in order to ensure the correctness of the constructor, we need to reassign the subroutine example as follows:
/***** Subclass of *****/
The following is a subclass constructor smart phone
function SmartPhone (OS)
{
This.os=os;

}
We use the mobile object as its prototype.
This means that instances of the new class will inherit Smartphone.prototype,
The latter was inherited by Mobile.prototype.
Mobile.prototype was inherited by Object.prototype.
Smartphone.prototype=new Mobile (Gsm,nokia);
Next add a new method to the subclass, send an email, just return the email address
Smartphone.prototype.sendemail=function (EmailAddress) {
Return this.emailaddress
}
The sub-class method above has a bit of flaw, because we explicitly set the Smartphone.prototype as we created an object, so we covered JS to provide
, and the given constructor attribute is discarded. This property refers to the constructor that created the object. But the Smartphone object integrates its
The constructor of the parent class, which does not have this property itself, explicitly sets a property to solve the problem:
Smartphone.prototype.constructor=smartphone;
var objsmartphone=new SmartPhone ();//instantiating subclasses

Http://www.cnblogs.com/tyrael007/archive/2006/09/22/511932.html



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.