You can use extends null .
Note that the class itself will still inherit from Function.prototype , not null . This way you can use function methods in the class.
But keep in mind that when using the extends clause, you must either initialize this before using it by calling super , or do not use this and return the object to the end.
In this case, you cannot initialize this with super , because Function.prototype not a constructor. Therefore, you will need to use Object.create to create an object that becomes an instance.
class Shape extends null { constructor(x, y) {
new.target will be the function that is being created. It may be Shape itself or another function that extends it. This is useful for expandability of Shape .
class Shape extends null { constructor(x, y) {
If you do not want to use this in your constructor, the alternative is an extension of the function, prototype is null . The downside is that your class inherits this function, and not directly from Function.prototype .
function NullClass() {} NullClass.prototype = null; class Shape extends NullClass { constructor(x, y) { super(); this.x = x; this.y = y; } move(x, y) { this.x += x; this.y += y; } } var rect = new Shape(0, 0); console.log(rect); console.log(Object.getPrototypeOf(rect) === Shape.prototype); console.log(Object.getPrototypeOf(Shape.prototype) === null); console.log(Object.getPrototypeOf(Shape) === NullClass); console.log(Object.getPrototypeOf(NullClass) === Function.prototype);
If you do not want to reuse NullClass , you can define it inline
class Shape extends Object.assign(function(){},{prototype:null}) { }
source share