Date

Object-oriented JavaScript for the C++ programmer.

This article presents a bestiary of JavaScript constructs, set against their equivalent in C++. Most of the basic class and object tools are available in JavaScript, and the syntax is often recognisably similar.

C++ is listed first in maroon followed by the corresponding JavaScript in dark green. For simplicity, C++ methods are defined inline.

Global variable:

int global =0;
var global =0;

Global function definition:

Note that the JavaScript syntax for a function definition is the same as a class definition:

int global_function(int i, int j)
{
    return 0;
}
function global_function(i,j)
{
    return 0;
}

Local variable:

A local variable uses the same syntax as a global, but it is declared within a function's scope.

void f()
{
    int local =0;
}
function f()
{
    var local =0;
}

Local static variable:

int f()
{
    static int static_local=0;
    static_local++;
    return static_local;
}
function f()
{
    if(typeof this.static_local == 'undefined')
        this.static_local=0;
    this.static_local++;
    return this.static_local;
}

Class definition:

Note that the JavaScript syntax for a class definition is the same as a function definition. Use it as a class definition by calling it with the new keyword to make a new object.

class C
{
};
function C()
{
}

Create a new object:

The new operator is crucial to object orientated JavaScript. In this example, C can be called as a normal global function, but when called with new it becomes the constructor for a new Object.

C* obj = new C();
var obj = new C();

member variable - PUBLIC:

The example includes a class constructor that initialises the member variable.

class C
{
public:
    int m_public;
    C(int i): m_public(i) {}
};
function C(i)
{
    this.m_public = i;
}

member variable - PRIVATE:

The example includes a class constructor that initialises the member variable. I don't recommend using this in JavaScript - it works for simple classes, but breaks down if you try to use inheritance (JavaScript prototypes).

class C
{
    int m_private;
public:
    C(int i): m_private(i) {}
};
function C(i)
{
    var m_private = i;
}

member variable - STATIC PUBLIC:

The example initialises the variable and also includes a class constructor that uses the variable.

class C
{
public:
    static int m_static;
    C()
    {
        m_static++;
    }
};

int C::m_static =0;
function C(i)
{
    C.m_static++;
}

C.m_static =0;

member function - PUBLIC:

The example is an accessor function for a private member variable.

class C
{
    int m_private;
public:
    C(int i): m_private(i) {}
    int accessor() { return m_private; }
};
function C(i)
{
    var m_private = i;
    this.accessor = function() { return m_private; }
}

member function - PRIVATE:

class C
{
private:
    void implementation() {}
};
// NO JAVASCRIPT EQUIVALENT

member function - STATIC PUBLIC:

The example is a simple factory function. Since the JavaScript function is defined outside the class, you cannot access any static private functions that the class may contain.

class C
{
public:
    C() {}
    static C* factory() { return new C(); }
};
C* obj = C::factory(); // called from outside
function C()
{
}
C.factory = function() { return new C(); }
var obj = C.factory(); // called from outside

member function - STATIC PRIVATE:

In Internet Explorer's JavaScript, nested functions are indeed private. Firefox and Safari allow you to call these functions from outside, but you must specify the fully scoped name (e.g. C.sum(1,2);).

class C
{
private:
    static int sum(int a, int b) { return a+b; }
public:
    C() {}
};
function C()
{
    function sum(a, b) { return a+b; }
}

Side-by-side Summary:

C++ JavaScript
int global_variable =0;

int global_function(int i)
{
    global_variable += i;
    return global_variable;
}

class C
{
private:
    int m_private;

public:
    int m_public;

    static int m_static;

    C(int i, int j):
        m_private(i),
        m_public(j)
    {
        m_static++;
    }

    int accessor()
    {
        return m_private;
    }

private:
    void implementation()
    {
    }

public:
    static C* factory()
    {
        return new C();
    }
};

int C::m_static =0;
var global_variable =0;

function global_function(i)
{
    global_variable += i;
    return global_variable;
}

function C(i,j)
{
    // (m_private not inheritable)
    var m_private = i;


    this.m_public = j;







    C.m_static++


    this.accessor = function()
    {
        return m_private;
    }


    function implementation()
    {
    }
} // end of C

C.factory = function()
{
    return new C();
}


C.m_static =0;

If you have any comments or corrections, then please leave a comment.