I took the method of formatting strings in JS from the answer to en.SO and adapted it to my needs. Initially, everything was written in global variables and worked.
But global variables are not good, added a wrapper function and exported.
/*Format string */ export function formatString() { if (!String.prototype.formatString) { String.prototype.formatString = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); }; } } In another script I imported and used:
import 'fileWithFormatStringMethod.js' .... a.href = canvas.toDataURL("image/{0}".formatString(dataFormat)); .... The console got the error:
Uncaught (in promise) TypeError: "image/{0}".formatString is not a function I tried to explicitly import the method in the calling file.
import { formatString } from 'fileWithFormatStringMethod.js' Classes are imported without problems from the same file.
import { elementByClass, elementById } from 'fileWithFormatStringMethod.js' Tell me what I did wrong?
import {formatString} from 'pathToFile';This must be done at the point of application initialization. and immediately after, call theformatStringmethod to override the method in theStringobject. But the approach itself is strange. Why not just make it a pure function? @ while1pass import only gives a link to the method, but does not comply with it - Vasily Barbashev