Multi-state implementation in JS

Source: Internet
Author: User

Polymorphic implementations are able to use and inherit similar methods. First, you define an abstract class. Some virtual methods are called. Virtual methods are not defined in abstract classes. Instead, it is implemented through its detailed implementation class.

Examples are as follows:

Object.extend=function (Destination,source) {in source) {Destination[property]=source[property];} return destination;} Defines an abstract base class base, no constructor function base () {};base.prototype={    initialize:function () {    this.oninit ();//calls a virtual method    }} function Subclassa () {//constructor}subclassa.prototype=object.extend ({propinsubclassa: "Propinsubclassa", OnInit: function () {alert (This.propinsubclassa);}},base.prototype); function Subclassb () {//constructor}subclassb.prototype= Object.extend ({propinsubclassb: "Propinsubclassb", Oninit:function () {alert (THIS.PROPINSUBCLASSB);}}, Base.prototype); var obja=new subclassa (); obja.initialize ();//Output "Propinsubclassa" var objb=new subclassb (); O Bjb.initialize ();//Output "PROPINSUBCLASSB"

First, an abstract base class base is defined, and the OnInit method is called in the Initialize method of the base class, but the implementation or declaration of the OnInit method is not used in the base class.

The Subclassa and SUBCLASSB classes inherit from the base class, and the OnInit method is implemented in different ways.

Multi-state implementation in JS

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.