Python has this beautiful function to turn this:
bar1 = \'foobar\'
bar2 = \'jumped\'
bar3 = \'dog\'
foo = \'The lazy \' + bar3 + \' \' + bar2 \' over the \'
JS:
String.prototype.format = function () {
var str = this;
for (var i = 0; i < arguments.length; i++) {
str = str.replace('{' + i + '}', arguments[i]);
}
return str;
}
bar1 = 'foobar';
bar2 = 'jumped';
bar3 = 'dog';
python_format = 'The lazy {2} {1} over the {0}'.format(bar1,bar2,bar3);
document.getElementById("demo").innerHTML = "JavaScript equivalent of Python's format() function:
" + python_format + "";
HTML:
CSS:
span#python_str {
color: red;
font-style: italic;
}
OUTPUT:
JavaScript equivalent of Python's format() function:
The lazy dog jumped over the foobar
DEMO:
jsFiddle