JS: Add Method to Prototype

By Xah Lee. Date: . Last updated: .

This page shows you how to add method to prototype objects.

Example. add method to string prototype

Example of adding a method to String.prototype

// add a removeVowels method for string prototype

String.prototype.removeVowels = function () {
  return this.replace(/[aeiou]/g, "");
};

// example call
console.log("how are you".removeVowels() === "hw r y");

🟢 tip: do not add to object prototype

Adding a method to prototype is not a recommended practice. Because, adding to global object changes the language unexpectedly and create conflicts if other people also do it.

[see Prototype and Inheritance]