If I have a function say:
var my_function = function()
{
}
If the function is not called, it is not taking up memory, it is just text sitting in memory.
However if you call it by say…
function_instance = new my_function();
It is instantiated is a sort of way, and the variables and methods it contains are loaded into memory.
Is this a way to represent a class/object model similar to C++?
Is my interpretation correct?
“a way to represent a class/object model similar to C++” would be through the use of prototypes.
As Kevin M pointed out, you can use the
thiskeyword to create instance variables in a function, like so:The problem however, that whenever you instantiate
my_function(), a new instance of themy_function.barfunction is also created. Enterprototypes:So, to sum it all up, the
prototypekeyword can be used to create function-specific, inheritable properties that are analoguous to C++’s member functions. Member functions of C++ aren’t instantiated for each instance of a class. Instead, the compiler adds athispointer to the function’s parameters; this pointer points to the instance that the member function is called on.More JSey fun to be had here: http://javascript.infogami.com/Javascript_in_Ten_Minutes