JS Object-Oriented Analysis

Source: Internet
Author: User

1. Anatomy of class structure

function fn () {

var variable = ""; Static properties

Function method () {}; Static methods

This.propertyname = value; Specific attributes

This.method = function () {};//specific method

}

PS: Static properties and static methods are used internally by the function FN and cannot be accessed by the outside world. Specific properties and specific methods are properties and methods that should be owned by the object created after the function is new, so that it can be accessed by the outside world. Without the new keyword, it would be considered a normal function call, and this point would be the Window object.

In addition, you can write this directly:

Fn.propertyname or Fn.method = function () {};

Like static variables in Java classes, the new-born object in JS cannot use the class's member variables, and classes and objects in Java can be used directly.

2. Rectangular Class

function Rect (width.height) {
This.r_width= width;
this.r_height= height;
This.desc = function () {
Return ' I am a rectangle oh ';
};
};

Extended Rectangle Calculation Area method
Rect.prototype.getArea = function () {
return this.r_width * this.r_height;
};

Print result function

Rect.prototype.toString = function () {
Alert ("The area of this rectangle is:" +this.getarea () + ", Width is:" +this.r_width+ ", height is:" +this.r_height);
};

3. Parallelogram class

function Square (value) {
This.s_width = value;
This.s_height = value;
};

Square.prototype = Rect.prototype; Assigning a Rect prototype object to square does not take the unique properties and methods of Rect and overrides the extended properties or methods in square. If you do not want to be overwritten, you can continue to add square.prototype.method= function{} in the back;

Square.prototype = new Rect ();//This notation will take the properties and methods specific to Rect, as well as the contents of the RECT central chain. At the same time also need to pay attention to square.prototype easily covered problems, generally written in the back extension.

For example:
Square.prototype.say = function () {
Return "I am a parallelogram";
};

You can also overwrite inherited properties and methods.

Square.prototype.getArea = function () {

return THIS.S_WIDHT * s_height;

}

JS Object-Oriented Analysis

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.