Please provide your thoughts with respect to Javascript only! I am aware of classes and classical inheritance but not at a great detail.
As far as I know, constructors are used as prototypes for other objects. For example, I could create a car constructor and give it objects such as hondaCivic, toyotaCamry, etc. Are there any other important things I should know about constructors?
Furthermore,
Constructor. The constructor method is a special method for creating and initializing an object created with a class . There can only be one special method with the name "constructor" in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.
In JavaScript, the Constructor method is invoked when you create an instance of a class. It is also used to initialize objects within a class. However, JavaScript will automatically create and execute an empty constructor if you have not defined any constructor method for a class.
A constructor is a special “MEMBER FUNCTION” in C++ that has the same name as the class it belongs to and is used to initialise some useful values for an object's data members. The constructor is used to INITIALIZE VALUES and is automatically called by the compiler, which is why.
The constructor() method is called automatically when a class is initiated, and it has to have the exact name "constructor", in fact, if you do not have a constructor method, JavaScript will add an invisible and empty constructor method. Note: A class cannot have more than one constructor() method.
A constructor is just a normal function. There is nothing special about it inherently.
All functions have a property called prototype
.
If you write
var myInstance = new MyFuction();
JavaScript does something special with your function when it executes it.
It sets the value of this
inside the function body to be myInstance
.
Additionally, it creates a reference to MyFunction.prototype
and stores it as an internal property of myInstance
.
When your code is executing, the rule that the interpreter follows is such that, if you try to access a property on myInstance
that it can't find, it will follow that reference to the function's prototype
and look there. This forms a chain, known as the prototype chain that leads all the way up to Object.prototype
.
Here's an example:
function Dog(name, breed) {
this.name = name;
this.breed = breed;
//if you don't specify a return value, `this` will be returned
}
Dog.prototype.speak = function() {
alert('Ruff, I\'m ' + this.name + ' the talking ' + this.breed);
}
var myDog = new Dog('buttercup', 'poodle');
myDog.speak();
The snippet above works, but if you run: console.log(myDog)
you'll see that it does not have a speak method. the speak method was found in it's prototype.
This means that all 'instances' of Dog
that are created will all share the same speak
method.
So, If I create another dog, it will be able to speak aswell:
var tommysDog = new Dog('rosco', 'pitbull');
tommysDog.speak(); //tommy's dog can speak too
function Dog(name, breed) {
this.name = name;
this.breed = breed;
//if you don't specify a return value, `this` will be returned
}
Dog.prototype.speak = function() {
alert('Ruff, I\'m ' + this.name + ' the talking ' + this.breed);
}
var myDog = new Dog('buttercup', 'poodle');
var tommysDog = new Dog('rosco', 'pitbull');
tommysDog.speak();
It also means that if I change the value of Dog.prototype.speak
at run time, all instances will be affected.
Note: technically, functions do have a constructor
property but it's not that important and it would just confuse you more if I tried to explain it here.
I suggest you read the mozilla docs
Additionally, I suggest you read this book. You'll learn a lot about proper design.
Also note that this is just one way to achieve code re-use. You don't have to go through prototypes at all. In fact JavaScript has methods that let you supply arbitrary values as the value of this
to functions as an argument.
This means that, even though my pet lizard can't normally speak, I can just borrow the method from Dog.prototype
by utilizing a method like call()
or apply()
. (All functions have these methods because they 'inherit' them from Function.prototype
.)
function Dog(name, breed) {
this.name = name;
this.breed = breed;
//if you don't specify a return value, `this` will be returned
}
Dog.prototype.speak = function() {
alert('Ruff, I\'m ' + this.name + ' the talking ' + this.breed);
};
function Lizard(name, species) {
this.name = name;
this.species = species;
}
Lizard.prototype.speak = function() {
var pretend_dog = { name: this.name, breed: this.species };
Dog.prototype.speak.call(pretend_dog);
};
var myLizard = new Lizard('larry', 'chamelion');
myLizard.speak();
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