I'm new to JavaScript and prototype-based programming. I know you can define inherited classes like class Circle extends Shape {}
, but class
is mostly a syntactic sugar of constructor functions, so is there any equivalent for inheritance of functions in JavaScript?
In other words, how can I define an inherited function without class
keyword, like this:
var Shape = function() {
this.getColor = function() {alert('Some color!')};
};
//Something like this:
var Circle = Function.create(Shape);
var circle1 = new Circle;
//With all below conditions:
console.log(Circle.__proto__ == Shape) // true
console.log(Circle.prototype.__proto__ == Shape.prototype) // true
console.log(circle1 instanceof Shape) // true
console.log(Object.getOwnPropertyNames(circle1)) // Array [ "getColor" ]
I've searched some articles but last condition wasn't achieved in them and getColor
was an inherited property, rather than being a member of the circle1
itself.