Re: degrees not radians
Lasse Reichstein Nielsen wrote:[color=blue]
>
> Fox <dont@spam.co m> writes:
>[color=green]
> > You can prototype every other object, including Object[/color]
>
> Object is not an object[1], it is a function/constructor. It can be
> used to create objects. You can change the prototype connected to
> *those* objects.[/color]
I think you confuse the method 'prototype' with the abstract concept of
"a prototype" or the definition of the constructor function.
To quote Netscape documentation:
"You can use the prototype to add properties or methods to all instances
of a class."
Oh -- and Object IS an object and it CAN be prototyped (as in, having
functionality added):
Object.prototyp e.something = function() { alert(this + "\nthis is
something"); }
function
test()
{
this.prop = "test";
}
var t = new test();
t.something(); // shows alert "[object Object] this is something"
(25).something( ); // shows alert "25 this is something"
etc...
something() is automatically inherited by EVERY instantiated object (and
object literal) after prototyping...
this is interesting:
Math.something( ) will display an alert [object Math] this is
something... (Not navigator or history, though, or any other object
"constructe d" before the prototyping [like DOM objects]).
[color=blue]
>
> Math, in comparison, is already an object, not a constructor. You
> don't have access to its constructor or its prototype (well, actually
> you do, because it's prototype is the same as for other objects
> created from Object (check "Math instanceof Object").[/color]
Math is a static class -- it has no constructor. It also has no
prototype method... you cannot:
Math.prototype. something
which is what I meant in my original statement.
[color=blue]
>[color=green]
> >(just about --
> > other exceptions: navigator, history)... so why not the Math object?[/color]
>
> Both exceptions are also singular objects, not objects that there are
> more than one of.[/color]
Both are static classes...no constructors...[I believe there are also
some "locks" on these, particularly history because of security concerns]
[color=blue]
>[color=green]
> > The error generated states that prototype is not a method of the
> > object.[/color]
>
> Because it isn't. There is no Math.prototype, because Math is not
> a function. It is an object.[/color]
okay, i'm getting bored.
[color=blue]
>[color=green]
> > prototyping is not instantiation (which, by the way, can be done -- even
> > though Math has no constructor) -- but it *does* make sense to be able
> > to add custom functionality -- which *is* the purpose of prototype.[/color]
>
> What *is* prototyping to you?[/color]
Just as the documentation has defined it:
to add properties or methods to all instances of a class
[color=blue]
>
> To me, a prototype based object oriented language (as opposed to a class
> based one) distinguishes itself in the way new objects are created.[/color]
No it doesn't... nothing distinguishing about it... it's just a
different approach -- the constructor IS the "class".[color=blue]
>
> In a class based language, new instances are based on a class.
> In a prototype based language, new instances are based on an object.[/color]
so far, the only difference is the use of the keyword "class" and an
internal constructor in a class, and the lack of one in a
prototyped-based OO language.
in a class-based language, new instances are objects -- storage is
allocated -- properties initialized -- methods placed in a "pool" (they
will not be declared again -- they are shared amongst all instances of
the class)
in a prototype-based language: new instances are objects -- storage is
allocated -- properties initialized -- methods placed in a pool
[supposedly] (they will not be declared again -- they are shared amongst
all instances of the main constructor -- whatever that might be at a
given time). Prototyping, under certain circumstances, is a way for us
to overload a "class" declaration. For example:
function
Car(arg)
{
this.make = arg;
}
function
Ford(arg)
{
this.model = arg;
}
Ford.prototype = new Car("Ford");
function
Chevy(arg)
{
this.model = arg;
}
Chevy.prototype = new Car("Chevy");
function
Other(arg1,arg2 )
{
this.country = arg1;
this.manufactur er = arg2;
}
Other.prototype = new Car("Foreign");
if we instantiate Ford or Chevy, then the resultant object is Car with
make and model properties. However, if we instantiate Other, then the
resultant object is Car with make, country, and manufacturer properties.
Overloading constructors is available in class-based languages, but the
prototype-based version is more flexible and powerful because only those
properties or methods required for that particular object instance are
created. In a class-based language, every possible property or method
that could be used must be declared.
[color=blue]
>
> That changing a property of the prototype of an object is also visible
> on the object, is not a requirement. It is one way to implement it,
> and the one often taken in dynamic languages where you can dynamically
> add properties.[/color]
This is where the true power of the prototype-based OO language
lies...absolute flexibility.. the ability to "morph" objects as you go
along (including core language objects).
[color=blue]
>
> "Prototypin g" is not distinct from instantiation to me, because
> prototyping is describing how instantiation happens.[/color]
yeah... i disagree -- the *constructor* describes how instantiation happens...
prototyping *changes* how instantiation happens -- when methods or
properties are prototyped to an object -- nothing in the rest of the
program changes... IF, and only if instantiation occurs (invoking a
constructor) will there be an affect.
[color=blue]
> So, the purpose of prototyping is *not* to be able to add properties
> to objects.[/color]
That's *exactly* the purpose of prototyping...
[color=blue]
> It is the ability to make objects from other objects.[/color]
adequately illustrated above...except that the objects and "other
objects" become one object (more like class/subclass).
One other thing:
even if you write a function to be a constructor for objects, it does
not make that the same object as those that would be instantiated from
it. In other words, the constructor exists as an instance of the
Function object (and as a member of the instantiated object), it is not,
for instance, a "Car" object (to use the above as an example)...thos e
will not exist until they are individually instantiated.
[color=blue]
>
> Adding properties to objects can be done directly on the objects.[/color]
Yeah --
[color=blue]
>
> The only place where prototyping and adding properties interact, is
> when you want to add properties to several related objects at once.
> There is only one Math object, so that is completely unnecessary.[/color]
There is only one String object, one Number object, one Array object,
etc... and yet, prototype is a method of those objects... it is
unnecessary because Math is not intended to be instantiated... but that
doesn't mean that, for consistency, it couldn't be used syntactically in
the same manner as these other core objects.
[color=blue]
>[color=green][color=darkred]
> >> No. You can add properties to Math as to any other object.[/color]
> >
> > The "standard" way is via prototype... (is my face turning blue?)[/color]
>
> I don't know, but you are wrong.[/color]
[color=blue]
> The "standard" way (if any such exist) to add properties to an
> object, is by assigning it directly to the object.[/color]
poor programming habits are rampant in JS programmers. The FAQ uses
prototype where appropriate (and correctly), not that I would tout the
faq as a guideline for programming style convention. However, that
flexibility exists, and it is anyone's prerogative to do so. In the case
of Math, it is a necessity.
[color=blue]
>[color=green]
> > I looked through this entire document...seve ral times... and didn't see
> > one instance where a property (OR a method) of Math had been overwritten
> > -- help me out here -- what's overwritten?[/color]
>
> I don't know, didn't check the page. But if you dind't find it, then
> make it yourself:[/color]
NOT ME -- the guy I was replying to claimed he had overwritten methods
in Math. Personally, I wouldn't care to do such a thing. There's nothing
that could be scripted that would be more efficient than what is available...
[color=blue]
>
> function changeMath() {
> var oldPI = Math.PI;
> var oldSin = Math.sin;
> Math.PI = 3;
> Math.sin = function(tr) {return oldSin(tr/3*oldPI);};
> }
>
> changeMath();
> alert([Math.sin(Math.P I),
> Math.cos(Math.P I)]);
> // normally alerts 0,-1
> // now alerts -0.1477...,-1
> // because Math.sin is changed but Math.PI is read only.
>[color=green]
> > I can be pretty dense sometimes -- I don't get this question. What else
> > *can* I do?[/color]
>
> Ok, why do you want to do it differently?[/color]
Do what differently? that section didn't make any sense to me.
[color=blue]
>[color=green]
> > I can add methods and properties and I can instantiate the
> > Math object.[/color]
>
> No, you never instantiated the Math object. It is not a constructor.[/color]
I didn't say I did instantiate the Math object... I know it's not a
constructor... I just said I could... I see you know how also... big deal.
[color=blue]
>[color=green]
> > I can have the static "instance" remain the same and
> > customize the instantiated object to the hilt (and it is actually *safe*
> > to override methods in the copy since the original static Math is still
> > there for backside support).[/color]
>
> Yes, that is fairly simple:
>
> function cloneMath() {
> function nop(){};
> nop.prototype = Math;
> return new nop();
> }
>[color=green]
> > What I want from Math is long double precision (64-bit). Just about
> > every platform has implemented it for at least the last 5 years (or
> > more!).[/color]
>
> Numbers in ECMAScript are 64-bit double precission IEEE-754 numbers.
> I don't know what the difference between that and 64-bit "long double"
> is.[/color]
I was wrong (I'm tired and stressed)... I meant 96-bit long doubles that
are available in C (at least in some compilers). It's been several years
since I've worked in C, so... I had to go back and look it
up...something I should have done before i made the statement.
[color=blue]
>[color=green]
> > With it, we could get considerably more precision. Astronomy
> > (solar system orbital mechanics) has been a long time programming
> > interest of mine (since the days of 8-bit processors). I want the
> > *option* to switch from standard 32-bit doubles to double precision long
> > doubles since it is not always necessary to run at such high precision
> > [sometimes I'll trade performance for precision.] It would be *nice* if
> > a selector option could be set so that trig functions would accept
> > either radian or degree arguments (or gradians).[/color]
>
> That should be possible, but you can do that yourself too. Not as
> efficiently, ofcourse.[/color]
That's the point -- efficiency... core routines exposed to js are much
more efficient than code we script.
[color=blue]
>
> Math.inputMode = 0; // 0 is radians, 1 is degress:
> function dispatch(func) {
> return function(n){swi tch (this.inputMode ) {
> case 1: return this.call(func, n/180*Math.PI);
> default: return this.call(func, n);
> }};
> }
> Math.sin = dispatch(Math.s in);
> Math.cos = dispatch(Math.c os);
> Math.tan = dispatch(Math.t an);
>[color=green]
> > I would like a *native* log (base 10) method.[/color]
>
> Is the efficiency of:
> Math.log10 = function(n) {return Math.log(n)*Mat h.LOG10E;}
> that bad?[/color]
Depends on how many times the function (or this in conjunction with many
other functions) needs to be iterated (and in some applications, that
could be hundreds or more times in a single loop). Extra (scripted) fp
multiplies tend to be a drag.
It's somewhat ridiculous that log base 10 is not available in the core language.
[color=blue]
>[color=green]
> > I would like *native* precision handlers (anything
> > scripted is considerably slower). I would like a native fixed point and
> > integer math package. How about Hyperbolic functions? Matrix operations?[/color]
>
> You want more. No problem with that :) You just shouldn't hold your
> breath waiting for a high-performance super math package to be
> implemented in a browser scripting language. It's not that much in
> demand.[/color]
no sh**.
[color=blue]
>
> /L
> --
> Lasse Reichstein Nielsen - lrn@hotpop.com
> DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
> 'Faith without judgement merely degrades the spirit divine.'[/color]
Lasse Reichstein Nielsen wrote:[color=blue]
>
> Fox <dont@spam.co m> writes:
>[color=green]
> > You can prototype every other object, including Object[/color]
>
> Object is not an object[1], it is a function/constructor. It can be
> used to create objects. You can change the prototype connected to
> *those* objects.[/color]
I think you confuse the method 'prototype' with the abstract concept of
"a prototype" or the definition of the constructor function.
To quote Netscape documentation:
"You can use the prototype to add properties or methods to all instances
of a class."
Oh -- and Object IS an object and it CAN be prototyped (as in, having
functionality added):
Object.prototyp e.something = function() { alert(this + "\nthis is
something"); }
function
test()
{
this.prop = "test";
}
var t = new test();
t.something(); // shows alert "[object Object] this is something"
(25).something( ); // shows alert "25 this is something"
etc...
something() is automatically inherited by EVERY instantiated object (and
object literal) after prototyping...
this is interesting:
Math.something( ) will display an alert [object Math] this is
something... (Not navigator or history, though, or any other object
"constructe d" before the prototyping [like DOM objects]).
[color=blue]
>
> Math, in comparison, is already an object, not a constructor. You
> don't have access to its constructor or its prototype (well, actually
> you do, because it's prototype is the same as for other objects
> created from Object (check "Math instanceof Object").[/color]
Math is a static class -- it has no constructor. It also has no
prototype method... you cannot:
Math.prototype. something
which is what I meant in my original statement.
[color=blue]
>[color=green]
> >(just about --
> > other exceptions: navigator, history)... so why not the Math object?[/color]
>
> Both exceptions are also singular objects, not objects that there are
> more than one of.[/color]
Both are static classes...no constructors...[I believe there are also
some "locks" on these, particularly history because of security concerns]
[color=blue]
>[color=green]
> > The error generated states that prototype is not a method of the
> > object.[/color]
>
> Because it isn't. There is no Math.prototype, because Math is not
> a function. It is an object.[/color]
okay, i'm getting bored.
[color=blue]
>[color=green]
> > prototyping is not instantiation (which, by the way, can be done -- even
> > though Math has no constructor) -- but it *does* make sense to be able
> > to add custom functionality -- which *is* the purpose of prototype.[/color]
>
> What *is* prototyping to you?[/color]
Just as the documentation has defined it:
to add properties or methods to all instances of a class
[color=blue]
>
> To me, a prototype based object oriented language (as opposed to a class
> based one) distinguishes itself in the way new objects are created.[/color]
No it doesn't... nothing distinguishing about it... it's just a
different approach -- the constructor IS the "class".[color=blue]
>
> In a class based language, new instances are based on a class.
> In a prototype based language, new instances are based on an object.[/color]
so far, the only difference is the use of the keyword "class" and an
internal constructor in a class, and the lack of one in a
prototyped-based OO language.
in a class-based language, new instances are objects -- storage is
allocated -- properties initialized -- methods placed in a "pool" (they
will not be declared again -- they are shared amongst all instances of
the class)
in a prototype-based language: new instances are objects -- storage is
allocated -- properties initialized -- methods placed in a pool
[supposedly] (they will not be declared again -- they are shared amongst
all instances of the main constructor -- whatever that might be at a
given time). Prototyping, under certain circumstances, is a way for us
to overload a "class" declaration. For example:
function
Car(arg)
{
this.make = arg;
}
function
Ford(arg)
{
this.model = arg;
}
Ford.prototype = new Car("Ford");
function
Chevy(arg)
{
this.model = arg;
}
Chevy.prototype = new Car("Chevy");
function
Other(arg1,arg2 )
{
this.country = arg1;
this.manufactur er = arg2;
}
Other.prototype = new Car("Foreign");
if we instantiate Ford or Chevy, then the resultant object is Car with
make and model properties. However, if we instantiate Other, then the
resultant object is Car with make, country, and manufacturer properties.
Overloading constructors is available in class-based languages, but the
prototype-based version is more flexible and powerful because only those
properties or methods required for that particular object instance are
created. In a class-based language, every possible property or method
that could be used must be declared.
[color=blue]
>
> That changing a property of the prototype of an object is also visible
> on the object, is not a requirement. It is one way to implement it,
> and the one often taken in dynamic languages where you can dynamically
> add properties.[/color]
This is where the true power of the prototype-based OO language
lies...absolute flexibility.. the ability to "morph" objects as you go
along (including core language objects).
[color=blue]
>
> "Prototypin g" is not distinct from instantiation to me, because
> prototyping is describing how instantiation happens.[/color]
yeah... i disagree -- the *constructor* describes how instantiation happens...
prototyping *changes* how instantiation happens -- when methods or
properties are prototyped to an object -- nothing in the rest of the
program changes... IF, and only if instantiation occurs (invoking a
constructor) will there be an affect.
[color=blue]
> So, the purpose of prototyping is *not* to be able to add properties
> to objects.[/color]
That's *exactly* the purpose of prototyping...
[color=blue]
> It is the ability to make objects from other objects.[/color]
adequately illustrated above...except that the objects and "other
objects" become one object (more like class/subclass).
One other thing:
even if you write a function to be a constructor for objects, it does
not make that the same object as those that would be instantiated from
it. In other words, the constructor exists as an instance of the
Function object (and as a member of the instantiated object), it is not,
for instance, a "Car" object (to use the above as an example)...thos e
will not exist until they are individually instantiated.
[color=blue]
>
> Adding properties to objects can be done directly on the objects.[/color]
Yeah --
[color=blue]
>
> The only place where prototyping and adding properties interact, is
> when you want to add properties to several related objects at once.
> There is only one Math object, so that is completely unnecessary.[/color]
There is only one String object, one Number object, one Array object,
etc... and yet, prototype is a method of those objects... it is
unnecessary because Math is not intended to be instantiated... but that
doesn't mean that, for consistency, it couldn't be used syntactically in
the same manner as these other core objects.
[color=blue]
>[color=green][color=darkred]
> >> No. You can add properties to Math as to any other object.[/color]
> >
> > The "standard" way is via prototype... (is my face turning blue?)[/color]
>
> I don't know, but you are wrong.[/color]
[color=blue]
> The "standard" way (if any such exist) to add properties to an
> object, is by assigning it directly to the object.[/color]
poor programming habits are rampant in JS programmers. The FAQ uses
prototype where appropriate (and correctly), not that I would tout the
faq as a guideline for programming style convention. However, that
flexibility exists, and it is anyone's prerogative to do so. In the case
of Math, it is a necessity.
[color=blue]
>[color=green]
> > I looked through this entire document...seve ral times... and didn't see
> > one instance where a property (OR a method) of Math had been overwritten
> > -- help me out here -- what's overwritten?[/color]
>
> I don't know, didn't check the page. But if you dind't find it, then
> make it yourself:[/color]
NOT ME -- the guy I was replying to claimed he had overwritten methods
in Math. Personally, I wouldn't care to do such a thing. There's nothing
that could be scripted that would be more efficient than what is available...
[color=blue]
>
> function changeMath() {
> var oldPI = Math.PI;
> var oldSin = Math.sin;
> Math.PI = 3;
> Math.sin = function(tr) {return oldSin(tr/3*oldPI);};
> }
>
> changeMath();
> alert([Math.sin(Math.P I),
> Math.cos(Math.P I)]);
> // normally alerts 0,-1
> // now alerts -0.1477...,-1
> // because Math.sin is changed but Math.PI is read only.
>[color=green]
> > I can be pretty dense sometimes -- I don't get this question. What else
> > *can* I do?[/color]
>
> Ok, why do you want to do it differently?[/color]
Do what differently? that section didn't make any sense to me.
[color=blue]
>[color=green]
> > I can add methods and properties and I can instantiate the
> > Math object.[/color]
>
> No, you never instantiated the Math object. It is not a constructor.[/color]
I didn't say I did instantiate the Math object... I know it's not a
constructor... I just said I could... I see you know how also... big deal.
[color=blue]
>[color=green]
> > I can have the static "instance" remain the same and
> > customize the instantiated object to the hilt (and it is actually *safe*
> > to override methods in the copy since the original static Math is still
> > there for backside support).[/color]
>
> Yes, that is fairly simple:
>
> function cloneMath() {
> function nop(){};
> nop.prototype = Math;
> return new nop();
> }
>[color=green]
> > What I want from Math is long double precision (64-bit). Just about
> > every platform has implemented it for at least the last 5 years (or
> > more!).[/color]
>
> Numbers in ECMAScript are 64-bit double precission IEEE-754 numbers.
> I don't know what the difference between that and 64-bit "long double"
> is.[/color]
I was wrong (I'm tired and stressed)... I meant 96-bit long doubles that
are available in C (at least in some compilers). It's been several years
since I've worked in C, so... I had to go back and look it
up...something I should have done before i made the statement.
[color=blue]
>[color=green]
> > With it, we could get considerably more precision. Astronomy
> > (solar system orbital mechanics) has been a long time programming
> > interest of mine (since the days of 8-bit processors). I want the
> > *option* to switch from standard 32-bit doubles to double precision long
> > doubles since it is not always necessary to run at such high precision
> > [sometimes I'll trade performance for precision.] It would be *nice* if
> > a selector option could be set so that trig functions would accept
> > either radian or degree arguments (or gradians).[/color]
>
> That should be possible, but you can do that yourself too. Not as
> efficiently, ofcourse.[/color]
That's the point -- efficiency... core routines exposed to js are much
more efficient than code we script.
[color=blue]
>
> Math.inputMode = 0; // 0 is radians, 1 is degress:
> function dispatch(func) {
> return function(n){swi tch (this.inputMode ) {
> case 1: return this.call(func, n/180*Math.PI);
> default: return this.call(func, n);
> }};
> }
> Math.sin = dispatch(Math.s in);
> Math.cos = dispatch(Math.c os);
> Math.tan = dispatch(Math.t an);
>[color=green]
> > I would like a *native* log (base 10) method.[/color]
>
> Is the efficiency of:
> Math.log10 = function(n) {return Math.log(n)*Mat h.LOG10E;}
> that bad?[/color]
Depends on how many times the function (or this in conjunction with many
other functions) needs to be iterated (and in some applications, that
could be hundreds or more times in a single loop). Extra (scripted) fp
multiplies tend to be a drag.
It's somewhat ridiculous that log base 10 is not available in the core language.
[color=blue]
>[color=green]
> > I would like *native* precision handlers (anything
> > scripted is considerably slower). I would like a native fixed point and
> > integer math package. How about Hyperbolic functions? Matrix operations?[/color]
>
> You want more. No problem with that :) You just shouldn't hold your
> breath waiting for a high-performance super math package to be
> implemented in a browser scripting language. It's not that much in
> demand.[/color]
no sh**.
[color=blue]
>
> /L
> --
> Lasse Reichstein Nielsen - lrn@hotpop.com
> DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
> 'Faith without judgement merely degrades the spirit divine.'[/color]
Comment