Help us understand the problem. What is going on with this article?

JavaScript作業MEMO バッククオートを使用し綺麗にまとめる

More than 1 year has passed since last update.

バッククオートを使用し綺麗にまとめる

xxx.js
function buildHTML(user) {
  var user_name = user.name
  var member = $('<div class="chat-group-user__name">').append(user_name);
  var add_button = $('<div class="chat-group-user__btn chat-group-user__btn--add">').append("テキスト");
  var list = $('<li class="chat-group-user">').append(member, add_button);
  $(".member-list").append(list);
}

バッククウォートを使用し、綺麗にまとめると以下のようになる。

xxx.js
function buildHTML(user) {
    var user_name = user.name
    var list = $(`
      <li class="chat-group-user">
        <div class="chat-group-user__name">${user_name}</div>
        <div class="chat-group-user__btn chat-group-user__btn--add">テキスト</div>
      </li>
      `);
    $(".member-list").append(list);
}

Point
・「.append」を多用しないのでパッと見てわかりやすい
・改行をそのまま書くことができる
・${~} の中には、変数や計算式を入れられる。

参考リンク

JavaScript の テンプレートリテラル を極める!
もうはじめよう、ES6~ECMAScript6の基本構文まとめ(JavaScript)~

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away