JS How to use apply to implement the inheritance

Source: Internet
Author: User

JS is how to implement inheritance using apply:
JavaScript is an object-oriented language, of course, inheritance is one of its important characteristics, such as the general can use the prototype implementation of inheritance, but the use of apply but can implement inheritance, the following through the code example introduced here, about the Apply function is not introduced here, specifically, you can see The role and difference of the JavaScript call () and apply () section. Here's a look at the code example:

functionParent (username) { This. username=username;  This. sayhello=function() {alert ( This. username); } }   functionChild (Username,password) {parent.apply ( This,NewArray (username));  This. password=password;  This. sayworld=function() {alert ( This. Password); } } varParent=NewParent ("Ant Tribe")); varChild=NewChild ("Antzone", "8888"); Parent.sayhello (); Child.sayhello (); Child.sayworld ();

The above code to achieve a simple inheritance effect, the code is relatively simple here is not much introduction, if there are any questions can be thread message.
For a pointer to this, you can refer to the This usage section of JavaScript .

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=11309

For more information, refer to: http://www.softwhy.com/javascript/

JS How to use apply to implement the inheritance

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.