PDA

View Full Version : Constructors, or "constructor:" ?



Mike Robinson
24 Sep 2009, 10:49 AM
In the tutorial, Extending Ext for Newbies (http://www.extjs.com/learn/Tutorial:Extending_Ext_for_Newbies), there is a fairly confusing ambiguity. Two different ways to define a constructor are shown:


In the first section, titled Constructor Model, we see a class "MyPanel" which includes a "constructor:" property in the hashref (oops, I mean "object" ;) )that is passed to Ext.extend.
But a mere two text-boxes later, in a section titled Extending Functionality, we see an altogether different way to define "MyPanel." In fact, the "constructor:" approach is not used again!

Reasonable question is, I think: "what's the difference?" Are they equivalent (and if so, why?). Is one or the other "incorrect," or "inferior?" What's the "best practice" here?

Animal
24 Sep 2009, 10:56 AM
First of all there's really no such thing as a constrictor in Javascript, it's just a function.

So what we are talking about is ways of assiging your class name to reference the function you write as teh constructor.

You can either just assign it:



MyClass = function(){...}


Or, Ext.extend offers a way to neatly wrap all this up, by returning whatever function it found in the constructor property, so



MyClass = Ext.extend(Ext.util.Observable, {
constructor: function(){...}
});


Does exactly the same thing.

It's just that with that second way, class extension is a one-step process. You define all the member functions in there too. The first way, you assign the constructor and THEN call Ext.extend.

Mike Robinson
24 Sep 2009, 12:09 PM
:-? Urgh! My head! :-?

Okay, staring once again at Ext.extend, I see how it works. It does seem to me, though, that you want to do it one way or the other.

:((Urgh!! My head-d-d-d-d!! :((

Am I correct that you should not do it both ways? What actually happens if you do? Let me guess:


We always start with "the function," which always contains whatever it contains. When you use the new operator, it will be executed.
When you "Extend" something, it always gets a "constructor" property, one way or the other. It's a dummy-function though, if there is nothing more to do.
If you do use the "constructor" property (on something you Extended...) you know that the property exists. What it does will add to what the JavaScript base function (that you defined) has already done.

Oui? Non?

Animal
24 Sep 2009, 9:32 PM
Ext.extend is very difficult to understand. It's worth revisiting it as you gain more experience with Javascript until you completely grasp it. Understanding it is a real test of the solidity of your Javascript knowledge.

When you extend, you always have a constructor. Obviously you need somethung to call with "new". If you choose to use the pure Ext.extend method, and you do not include a constructor property, then yes, it generates a dummy one which calls the superclass constructor for you.

It you include a constructor property, you must call the superclass constructor in it.

Mike Robinson
25 Sep 2009, 8:07 AM
I feel like I've been dunked in a bucket of Lisp. ;)

I spent quite some time reading and re-reading this article (http://trephine.org/t/index.php?title=JavaScript_classes), as well as that one (http://trephine.org/t/index.php?title=Understanding_the_JavaScript_new_keyword).

It is, as many others have said, "friggin' elegant."




That's it?
That is all.
Zen Master, I do not understand ...
A bathtub filled with brightly-colored machine tools.