JavaScript クリーン コードは、読みやすく、保守しやすく、効率的な JavaScript コードを作成するために使用できる一連の原則と実践方法です。クリーンなコードを記述することは、他の開発者がコードを理解し、変更しやすくなり、アプリケーションのパフォーマンスの向上にも役立つため、重要です。
このチュートリアルでは、より読みやすく、保守しやすく、効率的なコードを作成するのに役立つ 10 個の JavaScript のクリーンなコード例を学びます。このチュートリアルでは、関数、変数、オブジェクト、ループなどのさまざまなトピックを取り上げます。
a > b ? foo = 'apple' : foo = 'ball';
✔️
foo = a > b ? 'apple' : 'ball';
c > d ? a.foo = 'apple' : a.bar = 'apple';
✔️
a = { [c > d ? 'foo' : 'bar']: 'apple' };
export const foo;
export const bar;
export const kip;
✔️
export const foo, bar, kip;
const a = foo.x, b = foo.y;
✔️
const { ['x']: a, ['y']: b } = foo;
let a = foo[0], b = foo[1];
✔️
let [a, b] = foo;
const a = document.getElementById('a'),
b = document.getElementById('b'),
c = document.getElementById('c');
d = document.getElementById('d');
✔️
const elements = {};
['a', 'b', 'c', 'd'].forEach(item => elements = {
...elements,
[item]: document.getElementById(item)
});
const { a, b, c, d } = elements;
/*
For this to work your elements ID's must be
able to be valid Javascript variable names
and the names of the variables in which you store
your elements have to match with that elements' ID.
This is good for naming consistency & accesibility.
*/
if (foo) {
doSomething();
}
✔️
foo && doSomething();
if(!foo){
foo = 'apple';
}
bar(foo, kip);
✔️
bar(foo || 'apple', kip);
const SALARY = 150000000,
TAX = 15000000;
✔️
const SALARY = 15e7,
TAX = 15e6;
a = d;
b = d;
c = d;
✔️
a = b = c = d;
コーディングを楽しんでください!!!