2

I've been watching this video where Damian said that Crockford called it :" super constructor pattern"

Code sample : ( from the video)

var signalR;

signalR = function (url){
 return new signalR.prototype.init(url);
}

signalR.prototype={
 init:function(url)
 {
   this.url=url;
 } 
}

signalR.prototype.init.prototype = signalR.prototype;

Now, I've googled about Crockford and super constructor but all I could find is the implementation of Object.create :

Which I understand pretty clear : ( also it's gotchas)

function create(o)
{
 function f(){};
 f.prototype=o;
 return new f();
}

But still I don't see how it relates :

Question :

  • What exactly (in the video) - did he try to solve by using this pattern? ( also small code sample will be much appreciated).
Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    The only thing that this pattern enables is that you can call `signalR` without `new`. See also https://stackoverflow.com/questions/15591434/which-form-of-initialization-is-better – Bergi Apr 20 '14 at 17:57
  • @Bergi I believe there's a bit beyond that : for example - he instantiate the init method instead. so the init is the ctor function.....are you sure that's the only thing ? also it would be much appreciated if you convert this to a an answer .( p.s. the link is a bit realted but not 100% since here , as i said , he's dealing with init as the ctor function) – Royi Namir Apr 21 '14 at 07:03
  • Yes, he's using `init` as the constructor, which is an awful pattern. – Bergi Apr 21 '14 at 13:26
  • @Bergi Please I'm trying to learn here. can you please tell me why is it so awful ? or can you please provide a link describing this **exact** pattern ? – Royi Namir Apr 21 '14 at 17:43
  • See [this question](http://stackoverflow.com/q/12143590/1048572) - jQuery uses the same pattern. It is so awful because you shouldn't have an extra `.init` method, the `.constructor` function should take care of all that. The answer I linked first presents a much easier pattern for allowing the omission of the `new` keyword. – Bergi Apr 21 '14 at 18:57
  • @Bergi I don't see the point of this. why not jsut doing ctor function and new later like this http://jsbin.com/vajoredo/2/edit ? – Royi Namir Apr 23 '14 at 08:04
  • 1
    Sometimes as a pattern (who would use `new jQuery()` instead of just `$()`?), sometimes just to catch the mistake when someone forgets to prepend `new ` to a constructor call. [This also seeming to be the reason why Crockford dislikes `new`](http://stackoverflow.com/questions/6374809/is-javascript-s-new-keyword-considered-harmful-part-2). – Bergi Apr 23 '14 at 11:30

1 Answers1

3

Lets see at normal class with constructor and prototype

//-------------------
//---- Class signalr
//-------------------

//-- constructor
var signalr=function () {}

//--prototype
signalr.prototype.init=function (url)
    {
    this.url=url;
    }

signalr.prototype.test=function () {alert ("test");}

//-------------------
//---- Class signalr -- END
//-------------------

so to generate a new instance of this class we have to write the following.

var con=new signalr ();  //--- con would inherit two methods init and test
con.init ("yoururl");

Lets look at Crockford

//-------------------
//---- Class signalr - Crockford 
//-------------------

//-- signalr is here not the constructor, it's a normal function with a return value and setted prototype, the constructor becomes init which is defined in the signalr prototype
var signalr=function (url) 
    {
    return new signalr.prototype.init (url);
    }

//--prototype
//-----------

   //-- constructor
signalr.prototype.init=function (url)
    {
    this.url=url;
    }

signalr.prototype.test=function () {alert ("test");}
signalr.prototype.init.prototype=signalr.prototype       //- with this line, the prototype of the init method which is the constructor of our instances is linked to the prototype signalR so all instances would inherit the properties and methods defined in the signalR prototype

//-------------------
//---- Class signalr -- END
//-------------------

so to generate a new instance of this class we can write shortly the following to achieve the same like above.

var con=signalr ("yourURL");                //--- con inherits all the methods and properties of signalr.prototype with one line

seems that crockford is lazy to write lines, i'm thinking about pratical examples. but i think the whole thing is nothing exciting

  • I don't see what this would gain either. Why not call `this.init()` in the real constructor? – janfoeh Apr 20 '14 at 17:31
  • 1
    than you still have to write var con=new signalr("yourURL") ;))) as i said i think he is lazy in writing lines. but thats my opinion perhaps there something hidden about it but i dont think so –  Apr 20 '14 at 17:45
  • @ThorstenArtner'Austria' I don't know why he mentioned crockford. I didn't find a single article of him talking about this pattern. do you have a link ? – Royi Namir Apr 22 '14 at 18:29
  • Nope, i even dont know who crockford is, i was relying on your question, that this comes from crockford. –  Apr 22 '14 at 18:32
  • @ThorstenArtner'Austria' I don't see the point of this. why not jsut doing ctor function and new later like this http://jsbin.com/vajoredo/2/edit ? – Royi Namir Apr 23 '14 at 08:04