inheritance - How to create Abstract base class in JavaScript that can't be Instantiated -


i have class

function node() {     //implementation } 

and class

function attributionalnode() {     this.prototype.setattr = function (attr) {         this.attext = attr;     }; }  attributionalnode.prototype = new node(); attributionalnode.prototype.constructor = attributionalnode; 

how make class node() can't instantiated? e.g when try

var node = new node(); 

so throws exception?

this work:

function node() {      if (this.constructor === node) {          throw new error("cannot instantiate class");      }  }    function attributionalnode() {      node.call(this); // call super  }    attributionalnode.prototype = object.create(node.prototype);  attributionalnode.prototype.setattr = function (attr) {      this.attext = attr;  };  attributionalnode.prototype.constructor = attributionalnode;    var attrnode = new attributionalnode();  console.log(attrnode);  new node();

note: cannot refer this.prototype inside constructor, prototype property of constructor function, not of instances.

also, see here article on how extend js classes.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -