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 trial

JavaScript JavaScript Objects Object Basics Set the Value of Object Properties

Marius Kressin
Marius Kressin
10,038 Points

String Object

Is it possible to add a property to the string object, like

String.isAllUppercase = ( String === String.toUpperCase );

1 Answer

Steven Parker
Steven Parker
229,744 Points

For 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
Marius Kressin
10,038 Points

I don't think I'll understand this until a while later, but thank you anyway.