0

I want to define a property on the string constructor, and I want then to access this property from any file in my project. for example:

file1.js

String.capitalize = function(){
this.//...etc
}

file2.js

"Hello nice to meet you".capitalize()
Ekmek
  • 413
  • 2
  • 12

1 Answers1

0

You can define a new String prototype function like in the example below

String.prototype.capitalize = function(){
 return this.toUpperCase();
};




console.log("Hello nice to meet you".capitalize());
Aalexander
  • 4,987
  • 3
  • 11
  • 34
  • Why doesn't show code completion? and: do you think this is a bad practice? – Ekmek Jan 23 '21 at 15:22
  • I can really recommend reading the first answer here https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice – Aalexander Jan 23 '21 at 15:30