1

I'm learning OOP in Javascript and I came across one of the first examples and I'm unclear as to what the third line does ==> this.init.apply(this, arguments);

var Class = function(arguments){
    var klass = function(){
        this.init.apply(this, arguments);
    };
    klass.prototype.init = function(){};
    return klass;
};

And anyone mind explaining the whole thing? I understand most of it but I just want to feel like I'm thinking about this the right way.

kbourne
  • 19
  • 4
  • 1
    You have a `SyntaxError` in line 5, `function();` it's not valid, you ment `function() {};` ? – nicosantangelo Jun 29 '13 at 02:33
  • 1
    I too was confused by how all this OOP stuff in JavaScript worked. I would recommend watching [Crockford on Javascript: Act 3](http://www.youtube.com/watch?v=ya4UHuXNygM), as it was what helped a lot of this "click" for me. – Mark Hildreth Jun 29 '13 at 03:23

1 Answers1

2

Javascript has prototype inheritance. The code you pasted has a method called init declared as

klass.prototype.init = ...

Is like a public method of any copy of klass object. In Javascript every object is linked to a special object named "prototype". Everything you put inside that special object will be accesible to new instances of the original object.

Example:

var Human = function(n){
    this.name = n;
};
Human.prototype.getName = function(){
   return this.name;
};

//instance of Human
var pedro = new Human("Pedro");

Now pedro has access to Human.prototype objects...

pedro.getName();
//return "Pedro"

pedro prototype "inherits" from Human.prototype, and Human.prototype "inherits" from Object.prototype.

Lets get back to your example:

Each copy of klass will be linked with klass prototype besides having its own prototype. So every copy of klass (in this case: every object returned by Class) will be able to use the functions defined in the original klass prototype.

klass is an object (functions are objects in js) encapsulated inside the definition of another object/function named Class.

Class generate instances of klass because that is what Class function return.

This code ilustrates a way of defining a pseudo class "Class", with a method called init, accesible from any object returned by the function Class

That way you could do:

var myClass = new Class(anArrayWithArguments);

In the context of the function klass: "this" inside Class means the klass object, and this can call init as well as myClass can do it like new instances returned by Class function.

init can.be considered like a constructor in your example.

apply called from init allow to invoke init with an array of arguments over the object this (the new object to be returned by Class)

Guillermo Maschwitz
  • 1,066
  • 10
  • 16
  • *"Each function defined in the prototype of an object, as well as variables, are passed to each copy of the original object."* That sentence doesn't make sense to me. –  Jun 29 '13 at 02:40
  • 1
    ^ I think he meant "Each function/variable defined in the prototype of an object is inherited by any instance you make of that object". – Saturnix Jun 29 '13 at 02:48