VK wrote:
Richard Cornford wrote:
>function Point(a, b){
switch(typeof a){
case 'number':
this.set(a, b);
break;
case 'string':
this.setWithString(a);
break;
case 'object':
this.setWithPoint(a);
break;
default:
break;
}
}
Point.prototype.x = NaN; //or some other default value like zero.
Point.prototype.y = NaN;
Point.prototype.set = function(x, y){
this.x = x;
this.y = y;
};
Point.prototype.setWithString = function(coord){
this.x = Number(coord.charAt(0));
this.y = Number(coord.charAt(1));
};
Point.prototype.setWithPoint = function(point){
this.x = point.x;
this.y = point.y;
};
That is another viable approach.
So that garbage you posted is justified once again by your claim that it
"works", regardless of drunkenly it staggers while doing so?
What I don't like in it (doesn't mean
others have to share my feelings)
It would be amazing (and very unfortunate for them) if anyone else did.
- is that "obfuscates" the inheritance chain.
And how do you propose it is doing that? We understand that you are only
just becoming aware of prototypes in javascript and their significance
in inheritance, but that just means that what you may perceive as
obscure reflects only your very limited comprehension of the language.
Polymorphism is a horizontal process: from the entry point
it goes left, right or straight depending on arguments.
Inheritance is a vertical process (from parent to parent).
This way I don't see useful to enforce "vertical onto
horizontal".
What you don't see is a matter of no interest, particularly as it
results in your writing irrelevancies like that.
I have written one 'class' definition with a constructor that can take a
number of types of argument. That is all, and it is precisely what you
did, except your version was not informed by an understanding of
javascript.
As the result each Point instance is getting a full
set of sub-constructors from its constructor
Rubbish. Each - Point - instance is being defined with a set of 'setter'
methods. Those methods happen to be being employed by its contractor,
but there is no reason for not doing that.
"setter" (and "getter") methods are familiar concepts in OO programming,
and the OP will certainly have no trouble relating them directly to what
he has previously seen in Java. While your ragged collection of methods
of the constructor that can only be sensibly used with the -
apply/call - methods only pile needless complexity on the subject. They
are more a source of 'obfuscation' than anything I have written.
with future possible fanny actions like:
var p = new Point(2, 2);
var foo = new p.set(22, 22);
You blithering idiot. I know that you program without any understanding
of what the code you write is actually doing, and so might do something
as stupid as using the - new - operator on a function that is referred
to by the name "set" just because you can (and it will "work", by some
very broad definition of the word), but do you really think any real
programmers (and particularly experienced OO programmers) are going to
do anything so obviously stupid?
// foo has nothing to do with Point if anyone wonders.
And - p.set - is so obviously not intended to be used as a constructor
that nobody would expect it to.
So it's a bit like gluing on each newly born chicken the
leftovers of its egg - IMHO.
It is opinions like that that justify the observation that your opinions
are universally worthless.
Full disclosure: that's a historical moment when I do agree
with John G Harris :-)
You may think you do, but it is more likely that you did not understand
him.
I think that polymorphism in the programming is more of
an esthetical mannerism (see my cool vs. lame notes) rather
than something technically needed. That is not to target
to OP: please take it as an overall consideration.
That loose aggregation of words does not say one way or the other.
Richard.