0

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?

4 Answers4

3

In javascript, the class concept does not exist. Everything is an object. When you use the new operator it copies the prototype of that function into a new object. In other words, you can emulate what class can do in c++, but it's not a class.

Francis
  • 3,335
  • 20
  • 46
  • @Francis, right, he concept of "class" doesn't exist *yet* on the language, *constructor functions* are often called "classes" and although this may seem just a small terminology problem, it will be a real mess when the `class` syntax will be finally approved and standardized, probably for the next edition of ECMAScript, you won't know if somebody it talking about constructor functions or "real" ECMAScript classes... I don't agree with the *"Everything is an object."* part, the language has *primitive types*, and they are *not* objects, more info: http://stackoverflow.com/q/3907613#3907659 – Christian C. Salvadó Nov 16 '11 at 21:18
1

In JavaScript a new class is defined by creating a function. The function may contain other functions (methods), properties, etc.

When a function is called using the new operation the function becomes a constructor for that class. Inside the constructor the variable " this " is created and points to the object.

function Man(){
    this.name = 'John';
}

var person = new Man;
console.log(person.name);
Kevin M
  • 1,524
  • 17
  • 38
  • It is designed to behave like a class. They are pretty much the same thing. This is object oriented JavaScript. A nice link to read more about polymorphism, sub classing, prototyping, inheritance, etc http://mckoss.com/jscript/object.htm – Kevin M Nov 16 '11 at 21:06
1

"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 this keyword to create instance variables in a function, like so:

var my_function(foo)
{
  this.foo = foo;
  this.bar = function()
  {
    // bar-ing here
  }
}

The problem however, that whenever you instantiate my_function(), a new instance of the my_function.bar function is also created. Enter prototypes:

var barPrototype = { "bar" : function()
  {
    // bar-ing here
  }
};

var my_function(foo)
{
  this.foo = foo;
}
my_function.prototype = barPrototype;

So, to sum it all up, the prototype keyword 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 a this pointer 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

zyndor
  • 1,418
  • 3
  • 20
  • 36
0

As mentioned, Javascript functions can act as objects so Javascript can be object oriented but object inheritance is prototypical and not like C++. John Resig, the creator of jQuery, has done some work on emulating traditional classes and inheritance in javascript. You could take a look at his blog for an interesting example:

http://ejohn.org/blog/simple-javascript-inheritance/

Ben L
  • 2,491
  • 1
  • 16
  • 7