Skip to content

Variable

  • ใช้ camelCase เช่น firstName, isActive, totalCount
  • ใช้ชื่อที่สื่อความหมาย เช่น userName แทน u
  • ค่าคงที่ใช้ UPPER_CASE เช่น MAX_SIZE, API_KEY
js
const MAX_SIZE = 100;
const API_KEY = '1234567890';

Function

  • ใช้ camelCase และควรเป็นคำกริยา เช่น getData(), calculateTotal()
  • ฟังก์ชันที่คืนค่า boolean ควรเริ่มด้วย is, has, can เช่น isValid(), hasPermission()
  • ฟังก์ชัน callback อาจใช้ handle นำหน้า เช่น handleClick(), handleSubmit()
js
function getData() {
  // ...
}

function isValid() {
  // ...
}

function handleClick() {
  // ...
}

Object

  • ใช้ camelCase เช่น userProfile, configOptions
  • ใช้ชื่อที่เป็นคำนาม ที่สื่อถึงข้อมูลภายใน
  • Object literals ควรมีชื่อที่สื่อความหมายชัดเจน
js
const userProfile = {
  firstName: 'John',
  lastName: 'Doe',
  age: 25
};

Class

  • ใช้ PascalCase เช่น UserProfile, PaymentProcessor
  • ชื่อควรเป็นคำนามที่สื่อถึงลักษณะหรือความสามารถของ Class
  • ชื่อควรมีความเฉพาะเจาะจงพอที่จะเข้าใจหน้าที่ของ Class
js
class UserAccount {
  constructor(username, email) {
    this.username = username;
    this.email = email;
  }
  
  getDisplayName() {
    return this.username;
  }
}

const admin = new UserAccount('admin', '[email protected]');

File & Module

  • ใช้ kebab-case สำหรับไฟล์ เช่น user-profile.js, api-service.js
  • หรือใช้ PascalCase สำหรับ Component ใน React/Vue เช่น UserProfile.jsx
  • ใช้ชื่อที่สื่อถึงเนื้อหาหรือหน้าที่ของไฟล์
  • ไฟล์ที่ export Class ควรใช้ชื่อเดียวกับ Class
js
// user-profile.js
export class UserProfile {
  // ...
}
jsx
// UserProfile.jsx
export class UserProfile {
  // ...
}