LoginSignup
0
0

More than 1 year has passed since last update.

Eslintのエラーについて

Posted at

概要

webpackを利用してjQueryを記述中にeslintのエラーが出たので、エラーの意味と解決策

var storyTitle = $(".p-story").find(".js-blockTitle--scroll").html();
var storyTitleTrim = $.trim(storyTitle);
var newHtml2 = "";
storyTitleTrim.split("").forEach( v => {
  newHtml2 += "<span>" + v + "</span>";
});
$(".p-story").find(".js-blockTitle--scroll").html(newHtml2);
error  'storyTitle' is assigned a value but never used  no-unused-vars

→変数定義の際にvarを使用するのは望ましくないからconstかletで定義しようというエラー
varをconstもしくはletに変更するだけで解決。

const storyTitle = $(".p-story").find(".js-blockTitle--scroll").html();
const storyTitleTrim = $.trim(storyTitle);
const newHtml2 = "";
storyTitleTrim.split("").forEach( v => {
  newHtml2 += "<span>" + v + "</span>";
});
$(".p-story").find(".js-blockTitle--scroll").html(newHtml2);
error  'newHtml' is already defined                     no-redeclare

→「const newHtml2 = "";」はconstで変数宣言しているため、変数の再定義は不可というエラー
varだとまたエラーが出てしまうため、letにて変数宣言することで解決。

const storyTitle = $(".p-story").find(".js-blockTitle--scroll").html();
const storyTitleTrim = $.trim(storyTitle);
let newHtml2 = "";
storyTitleTrim.split("").forEach( v => {
  newHtml2 += "<span>" + v + "</span>";
});
$(".p-story").find(".js-blockTitle--scroll").html(newHtml2);
error  Unexpected string concatenation  prefer-template

→「newHtml2 += "" + v + "";」の記述について、javascriptの言語機能であるテンプレートリテラルにて記述しようというエラー。
newHtml2 += <span>${v}</span>;と記述することで解決。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0