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