I read about how the Javascript prototype property works along with inheritance, and then started looking at Angular.js code and came up with some questions.
First, I read that the prototype property points to an object that has a constructor property that points to the original function that is used to create the object. For example:
The prototype also contains any other methods or properties that were defined on it by us or the Javascript language itself, and they are shared by all instances of the object. If you want the object to inherit from Shape, you need to set the square prototype equal to the new Shape instance, because the internal [[prototype]] property for Square.prototype gets the value of the public object of the Shape.prototype property.
function Square() {} Square.prototype = new Shape(); var square = new Square(); square.position;
It all makes sense to me.
However, the Angular.js code I have a question for seems to be related to all of this, but does what I don't understand. It doesn't seem to be related to inheritance, so I can understand why there should be no differences, but I just wonder why they wrote it the way they did it.
Inside Angular.js there is a HashMap object and a Lexer object, but they are defined differently, but they seem to be created and used exactly the same. First, the Lexer constructor is defined, and then they install the prototype in the object literal containing the methods that should be used by all Lexer instances. All this makes sense. I donβt understand why they define the "constructor" property and set it only to "Lexer" if they are not for the HashMap below.
var Lexer = function(options) { this.options = options; };
Then, if you look at the HashMap code, they do the same, except that they do not specify a constructor property. Why is this? It seems to work the exact same way, and I tested that the constructor is still called.
So, the property of the constructor is optional if there is no inheritance, so maybe one person wrote Lexer and the other HashMap, and someone decided to specify the constructor?