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

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

node.js - Using Node without global install -

java - JavaScript + Thymeleaf - select onchange -