If I have a dummy constructor object:
function Circle()
{
this.radius = 3;
}
An instance of this object would have the single “radius” property. a) How do I query the constructor object for the number of properties it has?
b) How would I query Circle.prototype for the number of properties it has? Trying something like console.log(Object.getOwnPropertyNames(Circle.prototype)) doesn’t return anything
You have several terminology things wrong.
prototypeso if you were to iterate the object’s prototype, you would not see theradiusproperty.Assuming what you really meant to say is: “How do I iterate over the properties of an instance of my Circle object?”, the answer would be like this:
The prototype of an object is a different thing. You can think of it like a structure that every new instance of your object inherits automatically. You could use the prototype like this:
This will automatically give every instance of Circle, two methods calcArea and calcCircumference.
You can also add methods to the prototype of pre-existing objects that you do not have the code for such as Array (though you have to be careful when doing this). For example: