1

I got a JavaScript source code, and I don not understand something in this. Could you help me please, what does it mean?

Partnerek = (function () {

    Partnerek = function () { }

    //there are functions in this way
    //I think this is a public function
    Partnerek.foo = function foo()
    {
      //code
    }

    //there are functions in this way
    //this is a non public function
     function foosecondway()
    {
      //code
    }

    return Partnerek;
})()

It seems like a class, but this is not a class.

  1. What is this?
  2. Why Did the programmer write in this way?
  3. Is this a good way?
  4. What is the return value?
  5. Is the two function "Parameter" same in the code?
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Lajos
  • 2,549
  • 6
  • 31
  • 38
  • This is fairly typical modern Javascript code. Are you looking for a extensive tutorial in Javascript? There are lots of guides and tutorials over the net, but StackOverflow is not a good place to look for personal tutoring. – lanzz Nov 21 '12 at 12:07

1 Answers1

1

I'm not sure but to me it looks like it's a class buildup, which executes itself. so if you would call

var test = new Partnerrek()

This variable will become an instance of this class(This is done by the return) and the constructor

Partnerek = function(){}

Will be called.

Stijn_d
  • 1,078
  • 9
  • 20
  • Why is this way good? Is this slowwer, is not this? When I call this, this always calls the function, does not this? I have already tested, what will happen if I give the partnerek to the object. And I am able to use that like a class, but I did not use the new world, when I made that. nom I am a little bit confused? Can you explain this please, or tell something about it. Would the simple class way faster? – Lajos Nov 21 '12 at 12:22
  • 1
    what do you mean by simple class exactly? As Lanzz commented, this is a modern way to build a js-class. I've been checking it out a little more and I think that by wrapping a function around it, this class will have closure & private scope, which can come in quite handy, perhaps this explains it a little better: http://stackoverflow.com/questions/1114024/constructors-in-javascript-objects – Stijn_d Nov 21 '12 at 13:25