[javascript] 메서드와 함수의 차이점

메서드(Method)

예시:

const circle = {
  radius: 5,
  calculateArea: function() {
    return Math.PI * this.radius * this.radius;
  }
};
console.log(circle.calculateArea()); // 78.54

함수(Function)

예시:

function greet(name) {
  return `Hello, ${name}!`;
}
console.log(greet('John')); // Hello, John!

메서드와 함수는 각각 객체와 프로그래밍 언어의 특성에 따라 사용됩니다. 올바른 용도에 맞게 사용하여 코드를 구조화하는 데 도움이 됩니다.