Method for overwriting JavaScript calls

Source: Internet
Author: User

The jquery blog has been watching native JavaScript over the past two days. I have read some of the content that I haven't noticed before, and I will keep some handwriting in my blog for later viewing.
When a subclass defines a method with the same name as the superclass method, the subclass method overwrites the superclass method.
This is a common practice when creating subclass of an existing class. For example, if you define the tostring () method for a class at any time, the tostring () method of the object will be overwritten.
When a method overwrites another method, the former often needs to enhance the function of the method to be overwritten, rather than Completely replacing its function. To do this, the method must be able to call the method it overwrites. In a sense, this is a method chain, just like constructing a function chain. However, it is more difficult to call a overwritten method than to call a super-class constructor.
Give an example. For example, the rectange class has defined a tostring () method.
Rectange. Prototype. tostring = function (){
Return "[" + this. Width + "," + this. height + "]";
}
If a tostring () method is given to rectange, it must overwrite the tostring () method in positionedrectange so that a character in the subclass instance can reflect all its attributes, not just the width and height attributes. Positionedrectange is a very simple class. Its tostring () method can only return the values of all attributes. However, for example, Let's process the position attribute and delegate the width and height attributes to its superclass.
Rectange. Prototype. tostring = function (){
Return "(" + this. x + "," + this. Y + ")" +
Rectange. Prototype. tostring. Apply (this );
}
The implementation of the superclass tostring () is an attribute of the superclass prototype object. Note: you cannot directly call this method. Use Apply () to call this method, so that you can specify the object to call it.

Converting from jquery http://www.jqueryba.com/316.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.