Reading Notes on JavaScript object-oriented features

Source: Internet
Author: User

Learn the class object-oriented features of JavaScript;
1. The direct volume of objects is actually a combination of multiple value pairs, for example:
VaR Homer = {
Name: "Homer Simpson ",
Age: 34,
Married: True,
Email: homer@163.com"
}
2. attributes of an object: You can assign a value to a new attribute of the object to create it. For example:
VaR book = new object ();
Book. Title = "learn C #";
Book. Chapter1 = "class and object ";
Book. chapter1.page = 14;
Here, book is originally an object class object without any attributes, but here we add the title and Chapter1 attributes to it through the value assignment statement. Code You can start using these attributes.
Note that nesting of attributes is also used here;
This feature is amazing, and has never been seen in other languages;
3. A function is a value stored in a variable. You can assign a function to an attribute of an object to define a method. For example:
// Define a class rectangle
Function rectangle (W, H)
{
This. width = W;
This. Height = h;
}
// Define a function
Function compute_area ()
{
Return this. Width * This. height;
}
// Create a object
VaR page = new rectangle (10, 20 );
// Assign a function to a variable of the object
Page. Area = compute_area;
// Call the function through area;
VaR area = page. Area ();
Of course, the following definition will be more reasonable:

Function rectangle_resize (W, h) {This. Height = H; this. width = W ;}
Function rectangle_enlarge () {This. Height = This. Height * 2; this. width = This. Width * 2}
Function rectangle (W, H)
{
This. width = W;
This. Height = h;
This. Resize = rectangle_resize;
This. Enlarge = rectangle_enlarge;
}
4. Prototype)
After using a remote object, JavaScript is truly object-oriented in terms of expression;
Because the attributes of a prototype object are shared by all objects in the class, all attributes of all objects in the class are usually used to define the same attributes of all objects in the class. This makes the prototype object suitable for defining methods and some constants; (P144)
Javascript uses an inheritance mechanism based on the prototype object instead of a class-based Inheritance mechanism. In our understanding, JavaScript is not a real object-oriented language;
There is no real concept of classes in Javascript, But it simulates classes through constructors and prototype objects; so we can use it to simulate object-oriented;
The following is an example of a prototype object:
// Defines a constructor.
Function circle (r)
{
This. r = R;
}
// Defines a class attribute
Circle. Pi = 3.14159;
// Defines a class Method
Circle. max = function (a, B)
{
If (A. R> B. R)
Return
Else
Return B
}
// Defines an object method, Area
Circle. Prototype =
{
Area: function (){
Return circle. Pi * This. R * This. R;
}
}
// Use this class
VaR A = new circle (10 );
VaR B = new circle (20 );
Alert (A. Area ());
VaR c = circle. Max (A, B );
Alert (C. R );
5. class hierarchy
Generally, classes in JavaScript are directly subclasses of objects, which are unique to JavaScript. In general, there is no need for a complex class hierarchy;
However, sometimes we may still need some grassroots.
Because the inheritance system of JavaScript is based on prototype, we should start with prototype. If we want to add a subclass for the circle above, you only need to make the prototype object of anothercircle an instance of the circle class:
Function anothercircle (r)
{
This. r = R;
}
Anothercircle. Prototype = new circle (0 );
Anothercircle. Prototype. constructor = anothercircle;
6. Access the attributes of a text object using Arrays
For example, for the circle class above:
VaR c = new circle (1 );
Alert (C ["R"]);
If an object is in this form, we often call it an associated array;

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.