In Javascript, there are two ways you can define an object's properties:
First way
var myObject = {
property1: function () {..},
property2: function () {..}
};
Second Way
var myObject = {};
myObject.prototype.property1 = function() {...};
myObject.prototype.property2 = function() {...};
When I looked up prototype
online, I found that it is used to add properties to an existing object. So, if you have an object which was created by a third party library, then you can add extra properties to it. What I don't understand is why you would use prototype
for custom objects created in your own code. However, I have found many times (especially in Drupal core) that prototype
is used this way. Why is this?