<bdo id='Tqd2u'></bdo><ul id='Tqd2u'></ul>
  • <legend id='Tqd2u'><style id='Tqd2u'><dir id='Tqd2u'><q id='Tqd2u'></q></dir></style></legend>

    1. <tfoot id='Tqd2u'></tfoot>

    2. <small id='Tqd2u'></small><noframes id='Tqd2u'>

      <i id='Tqd2u'><tr id='Tqd2u'><dt id='Tqd2u'><q id='Tqd2u'><span id='Tqd2u'><b id='Tqd2u'><form id='Tqd2u'><ins id='Tqd2u'></ins><ul id='Tqd2u'></ul><sub id='Tqd2u'></sub></form><legend id='Tqd2u'></legend><bdo id='Tqd2u'><pre id='Tqd2u'><center id='Tqd2u'></center></pre></bdo></b><th id='Tqd2u'></th></span></q></dt></tr></i><div id='Tqd2u'><tfoot id='Tqd2u'></tfoot><dl id='Tqd2u'><fieldset id='Tqd2u'></fieldset></dl></div>

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

        时间:2023-09-05
            <tbody id='u0DKw'></tbody>

            • <tfoot id='u0DKw'></tfoot>

              <i id='u0DKw'><tr id='u0DKw'><dt id='u0DKw'><q id='u0DKw'><span id='u0DKw'><b id='u0DKw'><form id='u0DKw'><ins id='u0DKw'></ins><ul id='u0DKw'></ul><sub id='u0DKw'></sub></form><legend id='u0DKw'></legend><bdo id='u0DKw'><pre id='u0DKw'><center id='u0DKw'></center></pre></bdo></b><th id='u0DKw'></th></span></q></dt></tr></i><div id='u0DKw'><tfoot id='u0DKw'></tfoot><dl id='u0DKw'><fieldset id='u0DKw'></fieldset></dl></div>
            • <legend id='u0DKw'><style id='u0DKw'><dir id='u0DKw'><q id='u0DKw'></q></dir></style></legend>
              • <bdo id='u0DKw'></bdo><ul id='u0DKw'></ul>
                1. <small id='u0DKw'></small><noframes id='u0DKw'>

                  本文介绍了JavaScript 等效于 Python 的 format() 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  Python 有一个漂亮的函数来转这个:

                  Python has this beautiful function to turn this:

                  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 实现相同的语法?

                  Does JavaScript have such a function? If not, how would I create one which follows the same syntax as Python's implementation?

                  推荐答案

                  另一种方法,使用 String.prototype.replace 方法,第二个参数是replacer"函数:

                  Another approach, using the String.prototype.replace method, with a "replacer" function as second argument:

                  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"
                  

                  这篇关于JavaScript 等效于 Python 的 format() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JavaScript 格式数字,始终为 3 位数字 下一篇:JavaScript Number.toLocaleString() 分隔符后有 4 位数字

                  相关文章

                  最新文章

                  <legend id='x91sX'><style id='x91sX'><dir id='x91sX'><q id='x91sX'></q></dir></style></legend>

                    <small id='x91sX'></small><noframes id='x91sX'>

                  1. <i id='x91sX'><tr id='x91sX'><dt id='x91sX'><q id='x91sX'><span id='x91sX'><b id='x91sX'><form id='x91sX'><ins id='x91sX'></ins><ul id='x91sX'></ul><sub id='x91sX'></sub></form><legend id='x91sX'></legend><bdo id='x91sX'><pre id='x91sX'><center id='x91sX'></center></pre></bdo></b><th id='x91sX'></th></span></q></dt></tr></i><div id='x91sX'><tfoot id='x91sX'></tfoot><dl id='x91sX'><fieldset id='x91sX'></fieldset></dl></div>
                    1. <tfoot id='x91sX'></tfoot>
                        <bdo id='x91sX'></bdo><ul id='x91sX'></ul>