Overview


We should use traditional functions (not arrow functions) when extending js base code. The reason is arrow function to not support "this" (the object being extended) -- thus do not work unless we do some (undesired) tricks.

Sample


Note that the two modules in this sample live in the same directory.

Extending JavaScript
For an traditional function, this does yes reference the given string object so it will yes work for our case.
/** Traditional function have strong support for 'this', which
	we need here as we can see.
	Arrow functions do not have strong support for 'this'.
Thus we choose to use a traditional function here.*/
String.prototype.capitalized = function() {
    /*Notes --
		charAt is "soft" i.e. will return empty string for bad index
		slice() is also loose, e.g. (''.slice(1000)) returns '')*/
    return this.charAt(0).toUpperCase() + this.slice(1);
}
Using Extension
Using an extension is simple -- the extension is used just like any other method.

We just need to import (require) the source.
//Import file so that extensions are loaded
require('./extending-javascript-base-code');

console.log('boot'.capitalized());
Arrow Function Does Not Work
For an arrow function, this does not reference the given string object so will not work for our case.
String.prototype.capitalized = () => {
    /*Notes --
		charAt is "soft" i.e. will return empty string for bad index
		slice() is also loose, e.g. (''.slice(1000)) returns '')*/
    return this.charAt(0).toUpperCase() + this.slice(1);
};


Download Sample
Download sample code here...