함수와 방법 키워드 사이의 차이

2276 단어
방법은 대상에 속하는 함수다.방법은 수신기가 있지만 함수는 없습니다.
const someObject = {
  someMethod: function () {
    console.log('Hello, World!');
  },
};

// method call
someObject.someMethod(); // `someObject` is a receiver, `someMethod` is a method

const someFunction = someObject.someMethod;

// function invocation
someFunction(); // JavaScript functions are invoked without a receiver, using parenthesis syntax

function helloWorld() {
  console.log('Hello, World!');
}

helloWorld(); // logs "Hello, World!"

// When a function is declared on the global scope, it becomes a property of a global object
// The `window` is the receiver of `helloWorld,` which would make `helloWorld` a method and not a function. However, in JavaScript, it is still considered a function.
window.helloWorld(); // logs "Hello, World!"

좋은 웹페이지 즐겨찾기