Quick actions

cmd+k|ctrl+k

Navigation

Languages

JS prototype inheritance

Snippet info

Language

JavaScript

Visibility

public

Author

paulkotov

Created

2020-10-30T20:27:01Z

Updated

2021-04-21T17:40:30.96322Z

// Base class
function Alert (title) {
    if (!(this instanceof Alert)) {
        return new Alert();
    }
    this.title = title || 'alert';
}

// Base class method show
Alert.prototype.show = function () {
    console.log(this.title);
};

// class extends Base class
function SuccessAlert(title) {
    Alert.call(this, title);
    this.type = 'success';
}

// class methods of extending Base class
SuccessAlert.prototype = Alert.prototype;
SuccessAlert.prototype.on = function () {
    console.log('getting on');  
};

const message = new SuccessAlert('success');
message.show();
message.on();
INFO