Consider the following code:
var AryUser = new Array();
AryUser.prototype.HasUser = function() {
if(this.length > 0)
return true;
else
return false;
}
I am already using prototype extending on some objects in my Node.js Projects. I came across an article, which states it’s bad practice. Does it create overhead on server during execution of such functions?
Can anyone tell me what the reason is? (Any official document referral would be of great help)
You are not extending any prototype in the example you provided above. The
prototypeproperty only exists on functions –AryUseris an array. The above code will throw aReferenceError.You may wish to learn how prototypal inheritance works in JavaScript. Read the following answer: https://stackoverflow.com/a/8096017/783743
That being said it’s a bad practice to extend prototypes of native functions like
Function,ObjectandArray. This is because you might overwrite someone else’s function, possibly breaking their code.Extending native function prototypes is only recommended for monkey patching. Read the following article to understand the pros and cons of extending native function prototypes.