Last active
January 15, 2017 21:31
-
-
Save pertrai1/353377228fa788c8ab8a77189528743d to your computer and use it in GitHub Desktop.
JavaScript class from C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| This is the C++ example | |
| #include <iostream> | |
| using namespace std; | |
| class Base | |
| { | |
| public: | |
| int m_id; | |
| Base(int id = 0) : m_id(0) {} | |
| int getId() const { return m_id; } | |
| }; | |
| class Derived: public Base | |
| { | |
| public: | |
| double m_cost; | |
| Derived(double cost = 0.0, int id = 0) : Base(id), m_cost(cost) {} | |
| double getCost() const { return m_cost }; | |
| }; | |
| int main() | |
| { | |
| Derived derived(1.3, 5); | |
| cout << "ID: " << derived.getId() << '\n'; | |
| cout << "Cost: << derived.getCost() << '\n'; | |
| return 0; | |
| } | |
| **/ | |
| /** | |
| This is the JS from C++. Is this correct way to convert? | |
| **/ | |
| const _id = Symbol('id'); | |
| const _cost = Symbol('cost'); | |
| class Base { | |
| constructor(id = 0) { | |
| this[_id] = id; | |
| } | |
| get id() { | |
| return this[_id]; | |
| } | |
| }; | |
| class Derived extends Base { | |
| constructor(cost = 0.0, id = 0) { | |
| super(id); | |
| this[_cost]= cost; | |
| } | |
| get cost() { | |
| return this[_cost]; | |
| } | |
| }; | |
| const derived = new Derived(1.3, 5); | |
| console.log(`ID: ${derived.id}`); | |
| console.log(`Cost: ${derived.cost}`); | |
| console.log(Reflect.ownKeys(derived)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment