Adding methods to existing ECMAScript Objects

ES version 7 (2017) introduced two new Sting Methods padStart and padEnd, which cannot be used, since the Rhino Engine is only compatible with ES version 6 released in 2016.

I know, extending a base Object is not the best idea, but since it is limited to that ECMAScript, I believe it is worth a try and so I did:

I added the following code to my ECMAScript Object:

String.prototype.padStart = function(digits,fill){

        value = this;

        while (value.length < digits){

       value = fill+value;

   }

   return value

}

This results in the fact, I was able to use the padStart(x,y) Mehod on any Sting like:

testValue= '123'

testValue.padStart(5,'0') returned '00123' as planed

next I addedd the code for padEnd:

String.prototype.padEnd = function(digits,fill){

        value = this;

        while (value.length < digits){

       value = value + fill

   }

   return value 

}

As a result the first new method was still working, but when I called the padEnd() method I received the same result as the padStart!

Only defining the padEnd method, was working as expected!

Digging through the web I finally found an example, which led to the following code to introduche multiple methiods at once;

String.prototype = {

padStart: function(digits,fill){

        var value = this;

        while (value.length < digits){value = fill+value }

   return value

},

padEnd: function(digits,fill){

        var newValue = this;

        while (newValue.length < digits){newValue = newValue + fill}

   return newValue;

}

}

This code does not throw any error in the ECMAScript editor, but using the new methods on a string end with a function not found error.

Can anybody tell me, what I am doing wrong?

Kind regards

Thorsten