I have a class
function Node() { //implementation }
and another class
function AttributionalNode() { this.prototype.setAttr = function (attr) { this.atText = attr; }; } AttributionalNode.prototype = new Node(); AttributionalNode.prototype.constructor = AttributionalNode;
How to make class Node() so it can't be instantiated? e.g when I try
var node = new Node();
So it throws an Exception?
No, you cannot create an instance of an abstract class because it does not have a complete implementation. The purpose of an abstract class is to function as a base for subclasses. It acts like a template, or an empty or partially empty structure, you should extend it and build on it before you can use it.
Abstract classes need to be inherited and require subclasses to provide implementations for the method declared in the abstract class. As in Java, we have the abstract keyword to make a class an abstract class, there are no such reserve keywords in JavaScript to declare a class an abstract class.
Abstract Classes and Class Members An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
Put simply: when Rails creates a model as an abstract class it does so without an underlying table. That means that this model can never be instantiated.
In JavaScript engines that support ECMAScript 2015 (aka ES6) class syntax, this can be accomplished using the new.target
meta-property:
function Node() { if (new.target === Node) throw TypeError("new of abstract class Node"); }
or using class syntax:
class Node { constructor () { if (new.target === Node) throw TypeError("new of abstract class Node"); } }
in either case, just define AttributionalNode
as:
class AttributionalNode extends Node { constructor () { super(); } setAttr(attr) { this.atText = attr; } } new Node(); // will throw TypeError new AttributionalNode(); // works fine
For a more detailed explanation of new.target
see section 4.2 of this document.
This would work:
function Node() { if (this.constructor === Node) { throw new Error("Cannot instantiate this 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: you cannot refer to this.prototype
inside the constructor, as the prototype is only a property of the constructor function, not of the instances.
Also, see here for a good article on how to properly extend JS classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With