JavaScript是否等效于Python的format()函数?

搅拌机 :

Python具有以下功能:

bar1 = 'foobar'
bar2 = 'jumped'
bar3 = 'dog'

foo = 'The lazy ' + bar3 + ' ' + bar2 ' over the ' + bar1
# The lazy dog jumped over the foobar

变成这个:

bar1 = 'foobar'
bar2 = 'jumped'
bar3 = 'dog'

foo = 'The lazy {} {} over the {}'.format(bar3, bar2, bar1)
# The lazy dog jumped over the foobar

JavaScript有这样的功能吗?如果没有,我将如何创建一个遵循与Python实现相同的语法?

CMS:

使用该String.prototype.replace方法的另一种方法是将“替换器”功能作为第二个参数:

String.prototype.format = function () {
  var i = 0, args = arguments;
  return this.replace(/{}/g, function () {
    return typeof args[i] != 'undefined' ? args[i++] : '';
  });
};

var bar1 = 'foobar',
    bar2 = 'jumped',
    bar3 = 'dog';

'The lazy {} {} over the {}'.format(bar3, bar2, bar1);
// "The lazy dog jumped over the foobar"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

R是否等效于python的字符串`format`函数?

JavaScript等效于Python的参数化string.format()函数

R是否等效于Python函数“ dir”?

Python是否等效于R poly()函数?

python是否等效于Javascript的“ btoa”

在Python中是否有等效于R apply函数的函数?

等效于jquery / javascript中的PHP函数number_format

PHP是否等效于JavaScript的Array.prototype.some()函数

React和Javascript:是否等效于C函数原型?

Python是否等效于Matlab函数“填充”的灰度?

是否有R函数等效于Python中的range?

R是否等效于Python的'*'?

JavaScript中的python any()和all()函数等效于什么?

是否等效于Oracle中的cbind()函数?

MATLAB是否等效于R函数rank()?

JavaScript等效于printf / String.Format

使用Swift格式化是否等效于字符串函数String(format:...)

是否有一个PHP函数等效于JavaScript函数toLocaleString()

Dart 中是否有等效于 Javascript 的 `resolve` 函数(来自 Promise 构造函数)?

C#是否等效于JavaScript的encodeURIComponent()?

Javascript .apply()方法是否等效于Java?

JavaScript是否等效于JQuery AJAX请求?

是否有与 Python 的 getattr() 函数等效的 Javascript?

C ++中是否有任何等效于python中字典的get函数的函数?

Java的TreeSet是否等效于Python?

是否存在等效于Python的zip()的Java?

Python是否等效于Java的“密钥库”?

Clojure是否等效于Python的lxml库?

Ruby是否等效于Python的“ try”?