JavaScript Instance Methods

When reviewing MDN documentation I notice they use a specific nameing convention for instance methods e.g.,

Array.prototype.toString()

I’m sure this and other methods aren’t prototypes e.g., established for testing before becoming defacto method. So I’m really interested in understanding why this nameing convention is used.

Thanks in advance

Mark

JavaScript is a prototypal language in that all object types (aka, classes) have a prototype object that is inherited from the parent constructor. You’ll want to read up on PROTOTYPE and the prototype chain to which everything is attached.

constructor -> prototype object -> instance methods

In your example above, Array is the constructor, .prototype is attached object, and .toString() is the associated property (in this case a method).

The documentation is explicit in its description of the prototype object. For our purposes, we can access the methods of our instance directly, as we would the instance variables.

myArray = new Array(10)
myArray.fill(0)

.fill() is a method of the Array prototype inherited from the Array constructor.

This is well documented and more adequately explained on MDN, so be sure to chase down the description and examples to put all of this straight in your mind. We can cover more examples once you get to the unit on Classes. To learn the legacy implementation of constructor functions be sure to read up on ECMAScript 5, which is the basis of custom objects and core built in objects that predates class syntax.

1 Like