JSFiddle - React, Tailwind, and code Playground
JavaScript
function User(username,password){ //constructor
this.username = username;
this.password = password;
this.isLoggedIn = false;
}
User.prototype.login = function(){
alert('I\'m logging in with username:"' + this.username + '" and password:"' + this.password + '"');
this.isLoggedIn = true;
};
User.prototype.getPassword = function(){
if(!this.isLoggedIn){
throw 'You must login first';
}
return this.password;
};
User.prototype.logout = function(){
this.isLoggedIn = false;
};
function Admin(username,password){
if(!Admin.instance){
User.call(this,username,password);
Admin.instance = this;
}else{
throw 'You can have only one admin!';
}
}
Admin.getInstance = function(){
if(Admin.instance){
return Admin.instance;
}else{
throw 'You have no admin!';
}
}
Admin.instance = null;
Admin.prototype.login = function(){
alert('I\'m admin, I\'m logging in with username:"' + this.username + '" and password:"' + this.password + '"');
}
var user = new User('user','passwd');
user.login();
var admin = new Admin('admin','password');
admin.login();
new Admin('admin','password');