Today I saw a JavaScript template that I had never seen in my entire life. I can not say the purpose of using this template. It seems wrong to me, but I want to be a little conservative. This may be some amazing pattern that I have never seen before.
function Dog() { Dog.prototype.bark = function () { alert('woof!'); } this.bark = function () { Dog.prototype.bark(); } this.bark(); }
First, I'm not a fan of creating methods (as privileged members) inside the constructor for no reason. This will cause functions to be created each time an instance is created. Secondly, in this code snippet, it also calls the prototype name βDogβ instead of βthisβ. It bothers me.
Does anyone know what's good about it?
Thanks! Grace
javascript prototype-programming
Grace shao
source share