简体   繁体   中英

jquery/javascript function within string concatenation

i want to concatenation string having some calculated values in between, so i tried the code below:

var html = '<tr>' + 
          function () {
              var tds = '' 
              array.each(function (i, ele) { tds += '<td>' + i + '</td>' });
              return tds;
          } +
          '</tr>';

But its not working. The function i used in concatenation as treating like a string itself !. Is there any way to do that ? Thank you

You need to use an immediately-executing function:

var html = '<tr>' + 
      (function () {
          var tds = '' 
          array.each(function (i, ele) { tds += '<td>' + i + '</td>' });
          return tds;
      })() +
      '</tr>';

But you don't need this function, you can use existing functions to do what you want:

var html = '<tr>' + array.map(function(i, ele) {
    return '<td>' + i + '</td>';
}).join() +
    '</tr>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM