From: Asen Bozhilov on
Dmitry A. Soshnikov wrote:

> * Regarding to `_super()', it's useful to return the result of the
> super method call (maybe it will be needed for some reason);

Definitely. On my local machine, last version use:

Base.prototype._super = function(name)
{
var proto = this._proto,
parent = proto._parent;
delete proto._parent;
delete proto._proto;
var result = parent[name].apply(this, Array.prototype.slice.call
(arguments, 1));
proto._parent = parent;
proto._proto = proto;
return result;
};

> * For do not describe constructor function a methods separately,
> wrapper (builder) can be easily used;

Yes. With wrapper will be easy to do it that. But i want to keep the
simple code.

> * Also, regarding to `name' argument for the `_super()', nothing
> prevents to make it - if argument is - use it, else - use the same
> method of the parent prototype.

But that mean, extra logic for slicing arguments and pass proper
length of parameters to inherited method.

Regards.
From: Dmitry A. Soshnikov on
On Jan 16, 1:03 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote:
> Dmitry A. Soshnikov wrote:
> > * Regarding to `_super()', it's useful to return the result of the
> > super method call (maybe it will be needed for some reason);
>
> Definitely. On my local machine, last version use:
>
> Base.prototype._super = function(name)
> {
>   var proto = this._proto,
>       parent = proto._parent;
>   delete proto._parent;
>   delete proto._proto;
>   var result = parent[name].apply(this, Array.prototype.slice.call
> (arguments, 1));
>   proto._parent = parent;
>   proto._proto = proto;
>   return result;
>
> };
> > * For do not describe constructor function a methods separately,
> > wrapper (builder) can be easily used;
>
> Yes. With wrapper will be easy to do it that. But i want to keep the
> simple code.
>

It depends. For some, code with some builder-wrapper, is more simple
than to describe first constructor itself, than add methods
(especially if you have already `methods' method to add methods).

It's like e.g. in Ruby, where classes are just objects of class
`Class'. Regarding to ECMAScript wrapper for chaining prototypes it
can look similar.

> > * Also, regarding to `name' argument for the `_super()', nothing
> > prevents to make it - if argument is - use it, else - use the same
> > method of the parent prototype.
>
> But that mean, extra logic for slicing arguments and pass proper
> length of parameters to inherited method.
>

Yep, algorithm with parameters will be more complex. Anyway, that's
your decision ;)

/ds
From: Garrett Smith on
John G Harris wrote:
> On Wed, 13 Jan 2010 at 11:43:52, in comp.lang.javascript, David Mark
> wrote:
>
> <snip>
>> John Harris wrote a _good_ review of it, or a review that said it was
>> good? I suspect the former.
> <snip>
>
> Well, I wrote
> "I'll start by describing the content of the book, then follow this with
> some unenthusiastic comments."
> so you can decide for yourself :-) (By the way : I was being ultra
> polite as it was a public review.)
>
By good, I meant good in terms of reviewing objectively.

I found the review neither polite nor rude.

--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: John G Harris on
On Sat, 16 Jan 2010 at 02:19:47, in comp.lang.javascript, Thomas
'PointedEars' Lahn wrote:
>John G Harris wrote:
>
>> Thomas 'PointedEars' Lahn wrote:

<snip>
>> Javascript is more flexible.
>
>There is no "Javascript",
<snip>

Yes there is, but only at the beginning of sentences (in English).


>>> It allows calling the superordinate constructor to initialize those
>>> properties without having to keep track of API changes.
>>
>> You very definitely need to keep track of any parameter changes.
>
>True, but a good API change is one that does not make this a necessity as
>well.

Sounds like a pretty small change then. So small I wonder why the
function names have changed.

John
--
John Harris
From: Asen Bozhilov on
Dmitry A. Soshnikov wrote:

> It depends. For some, code with some builder-wrapper, is more simple
> than to describe first constructor itself, than add methods
> (especially if you have already `methods' method to add methods).
>
> It's like e.g. in Ruby, where classes are just objects of class
> `Class'. Regarding to ECMAScript wrapper for chaining prototypes it
> can look similar.

Thank for all of your responses and suggestions. That is my final
solutions.

var Class = (function()
{
var F = function(){},
Base = function(){};
Base.prototype = {
constructor : Base,
baseMethod : function(name)
{
var proto = this._proto,
parent = proto._parent;
delete proto._parent;
delete proto._proto;
var result = parent[name].apply(this, Array.prototype.slice.call
(arguments, 1));
proto._parent = parent;
proto._proto = proto;
return result;
},

base : function()
{
var args = [arguments.callee.caller._name];
return this.baseMethod.apply(this, args.concat.apply(args,
arguments));
},

addProperties : function(o)
{
for (var i in o)
{
if (o.hasOwnProperty(i))
{
var property = o[i];
if (typeof property == 'function')
{
property._name = i;
}
this[i] = property;
}
}

var jscript = ['valueOf', 'toString', 'constructor'];
for (i = 0; i < jscript.length; i++)
{
property = jscript[i];
if (o.hasOwnProperty(property))
{
this[property] = o[property];
o[property]._name = property;
}
}
}
};

return function(properties)
{
var extend = properties.extend || Base,
constructor = properties.constructor;
F.prototype = extend.prototype;
var p = constructor.prototype = new F();
p._proto = p;
p._parent = extend.prototype;

delete properties.extend;
p.addProperties(properties);

return constructor;
};
})();



/******** Example *******/
var A = Class({
constructor : function()
{
this.a_in_construct = 1;
},
test : function()
{
this.a_in_test = 1;
}
});

var B = Class({
extend : A,
constructor : function()
{
this.base();
this.b_in_construct = 2;
},
test : function()
{
this.base();
this.b_in_test = 2;
}
});

var C = Class({
extend : B,
constructor : function()
{
this.base();
this.c_in_construct = 3;
},
test : function()
{
this.baseMethod('test');
this.c_in_test = 3;
}
});

var o = new C();
window.alert(o.a_in_construct); //1
window.alert(o.b_in_construct); //2
window.alert(o.c_in_construct); //3

o.test();
window.alert(o.a_in_test); //1
window.alert(o.b_in_test); //2
window.alert(o.c_in_test); //3

Any suggestions, corrections and additions are welcome.