Front-end_dev

dynamically assigning name to function in ES6 & C++ 본문

ES6/Syntax

dynamically assigning name to function in ES6 & C++

Eat2go 2018. 12. 7. 18:32

Javascript V8 Engine은 C++기반으로 만들어져있다.

javasript에서 object의 property를 런타임시에 동적으로 마음대로 바꿀수잇다. 그럼 C++도 가능하다는뜻인데..

함수포인터로간단하게 구현할수있다.

아마 V8 Engine은 javascript object들을 map형태 혹은 다른 자료구조(테이블형식)로 관리하지않나 싶다.



Javascript


const str1 = 'this';
const str2 = 'isfunction';

class C {
m1() {
console.log('foo');
}
[str1]() {
console.log('bar');
}
[str1 + str2]() {
console.log('baz');
}
}





C++


#include <iostream>
#include <string>
#include <map>
using namespace std;

typedef void FP();

void m1() {
cout << "foo" << endl;
}
void m2() {
cout << "bar" << endl;
}
void m3() {
cout << "baz" << endl;
}

int main(int argc, char* argv[]) {
map<string, FP*> mymap;
string str1 = "this";
string str2 = "isfunction";

mymap["foo"] = m1;
mymap[str1] = m2;
mymap[str1+str2] = m3;

mymap["foo"]();
mymap[str1]();
mymap[str1+str2]();
}


'ES6 > Syntax' 카테고리의 다른 글

simple iterator(generator) example  (0) 2018.12.11
2차원배열 생성기 및 인덱스범위 벗어날시 에러 강제  (0) 2018.11.09
Promise 몸풀기  (0) 2018.08.06
ES7 decorator  (0) 2017.12.24
디자인패턴...  (0) 2017.04.05