JS - 함수

SeungHwan Lee·2021년 7월 15일
0

JS 시작하기

목록 보기
7/12


함수

특정 동작(기능)을 수행하는 일부 코드의 집합(부분)
function
// 함수 선언
function helloFunc() {
  // 실행코드
  console.log(1234);
}

// 함수 호출
helloFunc(); // 1234
// 함수 반환
function returnFunc() {
  return 123;
}

let a = returnFunc();

console.log(a); // 123
// 함수 선언!
function sum(a, b) { // a와 b는 매개변수 (Parameters)
  return a + b;
}

// 재사용!
let a = sum(1, 2); // 1과 2는 인수 (Arguments)
let b = sum(7, 12);
let c = sum(2, 4);

console.log(a, b, c); // 3, 19, 6
// 기명(이름이 있는) 함수
// 함수 선언!
function hello() {
  console.log('Hello~');
}

// 익명(이름이 없는) 함수
// 함수 표현!
let world = function () {
  console.log('World~');
}

// 함수 호출!
hello(); // Hello~
world(); // World~

// 객체 데이터
const jason = {
  name = 'JASON',
  age = 24,
  // 메소드 (Method)
  getName = function () {
    return this.name; // this 는 객체 내부의 라는 뜻
  }
};

const hisName = Jason.getName();
console.log(hisName); // JASON
// 혹은
console.log(jason.getName()); // JASON

0개의 댓글

관련 채용 정보