On Dec 24, 2010, at 4:27 PM, Michael Haufe (TNO) wrote:

> On Dec 24, 3:05 pm, Garrett Smith <dhtmlkitc...@gmail.com> wrote:
> 
>> I rather have it one way or the other. e.g.
>> 
>>   makePoint(x, y);
>> 
>>  - OR -
>> 
>>   new Point(x, y);
>> 
>> I just don't like seeing any extra if/else in the code. I also don't
>> want to handle the case where somebody might be relying on an anomaly
>> of calling the constructor as a function call.
> 
> If defensive programming isn't necessary, of course. But since JS
> can't statically enforce such things it may be necessary to do so.
> 
>>> Plus when using the constructor pattern, Java style "super" calls can
>>> be emulated with apply()
>> 
>> When Point has a superclass or when Point *is* the superclass?
> 
> In general I mean vs the Object.create() pattern.
> 
>> Generally I keep the constructors private and only use factories and
>> return an interface object. Doing that avoids having to deal with
>> constructor issues.
> 
> Just seems like more boilerplate when building a hierarchy as you'd
> have to manually set the constructor property and fidget with
> prototypes to make instanceof  and the constructor property work as
> expected. But I guess if its not expected to be used that way it
> wouldn't matter.
> 

What crockford is trying to point is that you should not think as the new 
operator as the classical use of new
you should think of it as the prototype pattern, and this is not in conflict in 
any way with the Object.create method

Object.create is good for creating inheritance hierarchies
new is good for creating instances that need an initializer function in this 
case known as constructor function.

with the case of super you don't need it because you cannot (even when there 
are implementations that provide a way to do it) mutate the proto attribute of 
an object, so you always know what object you come from. so

A <- B (B inherits from A)  just call  A.methodName from B.methodName
A = function(){}
A.prototype.methodName = function(){
        //code
};

B = function(){}
B.prototype = new A(); // many techniques here to avoid constructor function 
being executed if you want to
B.prototype.constructor = B;
B.prototype.methodName = function(){
        //your code
       A.prototype.call(this[, ArgumentList]);
};

you should follow the language idioms and not invent new ones because you don't 
understand something or because you don't like it, neither because you are 
trying to port the idioms from other languages.
well that is my point of view.


> -- 
> To view archived discussions from the original JSMentors Mailman list: 
> http://www.mail-archive.com/jsmentors@jsmentors.com/
> 
> To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/jsmentors@googlegroups.com/
> 
> To unsubscribe from this group, send email to
> jsmentors+unsubscr...@googlegroups.com

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to