JavaScript Object Constructor
In this article, I will explain Object Constructor property of JavaScript.In the world of OOP,We need to create an object that can be used multiple times without having to redefine the object every time.The standard way to achieve this is to use the Object Constructor function.
In the world of OOP,We need to create an object that can be used multiple times without having to redefine the object every time.The standard way to achieve this is to use the Object Constructor function.
The constructor property is a reference to the function that created an object.
The constructor property returns the function that created the array object's prototype.
It is supported in all major browsers.
E.g.
<script type="text/javascript">
//Below is a traditional way of writing a function.
//To write your own constructors, you use the this keyword within the constructor to refer to the newly-created object.
//The constructor is a function matching the name of the object
function person(Carname,carColor)
{
this.Carname=Carname;
this.carColor=carColor;
}
//A common way to create an instance of a JavaScript object is use the new operator.
//The new operator creates a new object based on a constructor and a prototype.
Car=new car("Indica","Silver");
document.write(Car.name + " is " + Car.carColor");
</script>
Note:The constructor can accept parameters however, it is not possible to overload the constructors by having multiple functions with a different number of parameters.