Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMarius Kressin
10,038 PointsString Object
Is it possible to add a property to the string object, like
String.isAllUppercase = ( String === String.toUpperCase );
1 Answer
Steven Parker
231,269 PointsFor practical purposes, it might be better to just define a function that can be passed a string argument:
const isAllUppercase = str => str == str.toUpperCase();
Yet you can modify Strings, but with a different and not quite so direct syntax. If you wanted to define a new method, it could be done like this:
String.prototype.isAllUppercase = function() { return this == this.toUpperCase(); };
But it looks like what you really want is a new "getter method" (one that can be invoked without parentheses), which requires an even more complex syntax.
Object.defineProperty(String.prototype, "isAllUppercase", {
get() { return this == this.toUpperCase(); }
});
Also note that either way, when you invoke toUpperCase
, you must add parentheses to its name.
Marius Kressin
10,038 PointsMarius Kressin
10,038 PointsI don't think I'll understand this until a while later, but thank you anyway.