JavaScript function 탐구 - 기본

FGPRJS·2021년 10월 20일
0

JavaScript function에 대한 여러가지 탐구를 작성한다


  • JavaScript의 Primitive Type

    • undefined
      • 설정하지 않음
    • null
      • 의도적으로 비워놓음
    • boolean
      • true/false
    • number
      • 모든 숫자
    • string
      • 문자열
    • object
      • 객체
    • (symbol)
      • (ES6+) 심볼
  • function은 상기 기재된 6종(ES6+:7종) 중에서 object에 해당한다.

function hello_world(){
    console.log("hello world!");
}

console.log(typeof hello_world); //function
console.log(typeof hello_world.prototype); //object
  • 그냥 출력하면 function이라는 type이 나오지만 그 근간은 object이다.
  • 결국 객체 object이기 때문에 객체처럼 쓸 수 있다.
let GreetingFactory = {
    hi: say_hi,
    hello: say_hello,
    bye: say_bye
}

function say_hi(){
    console.log("hi!");
}

function say_hello(){
    console.log("hello!");
}

function say_bye(){
    console.log("bye!");
}

GreetingFactory.hello(); //hello!
GreetingFactory.bye(); //bye!
GreetingFactory.hi(); //hi!
  • 참고) cpp의 함수 포인터
#include <string>
#include <iostream>

void hello_world() {
	std::cout << "hello world!" << std::endl;
}

int main() {
	
	auto myhelloworld1 = hello_world;
	void (*myhelloworld2)() = hello_world;
	
	std::cout << myhelloworld1 << std::endl; 
    	//n비트 가상 메모리 주소 ex)12345678
	std::cout << myhelloworld2 << std::endl; 
    	//위와 똑같은 n비트 가상 메모리 주소 ex)12345678
	myhelloworld1(); //hello world!
	myhelloworld2(); //hello world!

	return 0;
}
profile
FGPRJS

0개의 댓글