JavaScript: extend prototype dynamically bad practice?

I want to know if Function prototype extension is dynamic, is bad practice. I am going to do this using a static method that gets the property name and function to add to the prototype.

Is this a bad practice?

function Test() { this.condition = false; } Test.extend = function(id, task) { this.prototype[id] = function() { return task.apply(this, arguments); }; }; 
+5
source share
1 answer

I would say that this is bad practice in this case, since you have no control over the fact that the added method [id] becomes overridden inside the class.

 var test = new Test(); test.extend("example", function() { console.log("First Method"); }); test.extend("example", function() { console.log("Second Method"); }); 

Like your code, you do not know when the first method will be overridden and thus randomly breaks your code.

0
source

All Articles