Instance properties versus prototype properties
Instance properties are the properties that are part of the object instance itself, as shown in the following example:
function Player() { this.isAvailable = function() { return "Instance method says - he is hired"; }; } Player.prototype.isAvailable = function() { return "Prototype method says - he is Not hired"; }; var crazyBob = new Player(); console.log(crazyBob.isAvailable());
When you run this example, you will see that Instance method says - he is hired is printed. The isAvailable()
function defined in the Player()
function is called an instance of Player
. This means that apart from attaching properties via the prototype, you can use the this keyword to initialize properties in a constructor. When we have the same functions defined as an instance property and also as a prototype, the instance property takes precedence. The rules governing the precedence of the initialization are as follows:
Properties are tied to the object instance...