I'm trying to simulate the C# string.Format() in JS.
For this, I have an object called string and a function called Format() passing as parameter, in a variadic function, a string with its placeholders and also its values.
An example should be:
string.Format("{0} - {1}", "Hello", "World");
that must return me Hello - World.
Although, it gives me just "{undefined} - {undefined}". I'm using global modifier to get all, but it doesn't works.
var string = {
Format: function() {
var text = arguments[0];
for (i = 1; i < arguments.length; i++) {
var result = text.replace(/([0-9]+)/g, arguments["$1"]);
}
console.log(result);
}
}
Where is my error?
{..}thing, not just the number. c. you never modifytext, you never do anything withresult, you never return anything. d. I can't even imagine what you thinkarguments["$1"]means.