You want to add a new method called compareTo() to the default JavaScript's String object. How will you do it?
Explanation
The prototype property allows you to add properties and methods to an object.
Example:
object.prototype.name=value

Thanks a lot for such vast explanation, @docahrens ! Fixed, now question should be correct)

2016 Jun 15, 6:44:02 PM

Question ambiguous: You said you wanted to add method called compareTo to a 'single' String object, but String.proptype.compareTo will add compareTo to all the String objects from that point forward. Observe: var a = new String('test'); var b = 'testing'; console.log(a); console.log(b); String.prototype.compareTo = function() { console.log('comparing:' + this); } if (a.compareTo){ a.compareTo(); } if (b.compareTo) { b.compareTo(); } So, it did not change only a single String object as you said in your question, but all String objects. so, the way you have it worded, your "correct" answer is incorrect.

2016 Jun 10, 11:07:33 PM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback