Javascript Object learning notes (2)-object prototype, inheritance

Source: Internet
Author: User
Here we use the rectangle () function as an Example 1. simple Function functionrectangle (w, h) {this. widthw; this. heighth;} vartestnewrectangle (); create a simple rectangle object that includes the width and height attributes to define an additional calculation...

The rectangle () function is used as an example.
1. Simple Functions
Function rectangle (w, h ){
This. width = w;
This. height = h;
}
Var test = new rectangle (); create a simple rectangle object, including the width and height attributes
An additional function defining the calculated area transmits the test object as a parameter.
Function getRecArea (rec ){
Return rec. width * rec. height;
}
Console. log (getRecArea (test ));
In this case, you can simply calculate any simple rectangular area, but the above does not conform to the object-oriented programming design philosophy, the method should also be included in the "class"
Function rectangle (w, h ){
This. width = w;
This. height = h;
This. area = function () {return this. width * this. height}
}
Console. log (test. area ())
This is already a friendly solution, but it is not optimal. Each time a rectanlge object is created, the width and height parameters are changed, the arec functions are the same in each rectanlge object. This reminds me of the Inheritance Mechanism in java, which inherits from the base class in java, each object in javascript has prototype object attributes (in fact, after an object is created, the prototype attribute value of this object is initialized by default. prototype Initialization is an object and contains a constructor attribute, this is also why every object has a constructor). functions or attributes added from prototype will become attributes of the initialized object.
Function rectangle (w, h ){
This. width = w;
This. height = h;
}
Rectangle. prototype. arec = function () {return this. width * this. height;} // conclusion: it is a good solution to put unchanged attributes in prototype.

2. Extended built-in types
Not only do user-defined functions have prototype, but also built-in classes such as String and Date in javascript have prototype attributes;
Instance: determines whether a string ends with a specified character.
String. prototype. isEndWith = function (c ){
Return (c = this. charAt (this. length-1 ))
}
Var s = "STRINGc ";
Console. log (s. isEend ());

 

From: I can't say goodbye

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.