degrees not radians

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fox

    #16
    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]

    Comment

    • Fox

      #17
      Re: degrees not radians



      Dr John Stockton wrote:[color=blue]
      >
      > JRS: In article <3FE3EF31.61509 02E@spam.com>, seen in
      > news:comp.lang. javascript, Fox <dont@spam.co m> posted at Sat, 20 Dec
      > 2003 00:41:53 :-
      >[color=green]
      > >What I want from Math is long double precision (64-bit).[/color]
      >
      > You already have, in standard javascript, all numbers being IEEE 64-bir
      > Doubles, with 53 bits of numeric resolution. Have you not seen FAQ 4.7?[/color]

      Yeah I have -- the accuracy is only 14 significant digits, not decimal
      digits -- it's two different things and is precisely the issue of how
      numbers are stored in a floating point processor [similar to scientific
      notation -- where there is an implied one and the rest are (52) binary
      bits representing the fractional part from there...with 11 exponent bits
      and a sign bit]. With an FPU, you can accurately represent a number like
      1e-300, or 9.847385948392e-245, but you cannot accurately represent a
      number like:

      123400000.00000 000134341 -- too many significant digits.

      anything past 14 significant digits is just "binary noise" that doesn't
      translate to decimal.

      [color=blue]
      >
      > Or do you mean the 80-bit type, called "extended" in Pascal & Delphi?[/color]

      I actaully meant 96 bit long doubles as defined in the Metrowerks C
      compiler (fp.h) (Mac, at least, not sure about PC).
      I got ahead of myself (and i actually had to look it up in the header
      files)...I also used to work quite a bit with fixed point integer math
      and was writing a 64-bit version...proba bly what i got "stuck" on -- OR
      -- I associated the resolution of the fp's with older 32-bit machines as
      opposed to newer 64-bit machines -- long doubles more or less came into
      existence with the 64-bit machines (or 32/64's).

      96-bit long doubles would translate into, roughly, an extra 4 or 5
      (accurate) significant (decimal) digits.

      It's not like we have any choices in JS as we do in other languages.
      Thing is: I said long double and meant long double...howeve r many bits
      there are in JS floating point numbers is unimportant... they're still
      not enough for "accurate" astronomy (just "close enough"). It was an
      unfortunate mistake.
      [color=blue]
      >[color=green]
      > >Just about
      > >every platform has implemented it for at least the last 5 years (or
      > >more!). 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[/color]
      >
      > What could standard 32-bit doubles be? IEEE singles are 32-bit, with 24
      > bits of resolution.
      >[/color]

      same problem as above -- just adjusted down...
      at this point, it's an obvious mistake... you just like rubbing it in?

      [color=blue][color=green]
      > > 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). I would like a *native*
      > >log (base 10) method. 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]
      >
      > The proportion of javascript-enabled browser users who would be
      > interested in pages using that is very small.
      >[/color]
      perhaps... or maybe they're just resigned to the status quo.
      [color=blue]
      > The proportion of javascript authors who can use Math to the full is
      > very small.[/color]

      no doubt...
      [color=blue]
      >
      > The proportion of those who cannot implement those functions themselves,
      > or need speed greater that self-implementing would produce, is
      > insignificant - it is commercially-negligible. Therefore, you should
      > not expect to see it come.[/color]

      didn't say i ever expected it -- just what *i* would like.
      [color=blue]
      >
      > The only omission that would make commercial sense to rectify is the
      > lack of an ECMA-standard means to convert from Number to given-length
      > String with a given number of decimal places shown; for that, see the
      > FAQ.[/color]

      that's a formatting issue for the end of calculations... not really a
      critical speed issue.
      [color=blue]
      >
      > Please give a proper name - we are accustomed to seeing Fox Mahoney
      > here, and posting as Fox can cause confusion.
      >[/color]

      it's not like you know me personally...wh at difference does it make?

      [color=blue]
      > --
      > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      > <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      > <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      > <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.[/color]

      Comment

      • Fox

        #18
        Re: degrees not radians



        Dr John Stockton wrote:[color=blue]
        >
        > JRS: In article <3FE2804E.4627B 4A8@spam.com>, seen in
        > news:comp.lang. javascript, Fox <dont@spam.co m> posted at Thu, 18 Dec
        > 2003 22:36:30 :-
        >[color=green]
        > >Jeff Thies wrote:[color=darkred]
        > >>
        > >> Is it possible to set the Math functions to use degrees instead of
        > >> radians?
        > >>
        > >> I'm working out some astronomical calculations and everything is in
        > >> degrees. All those conversions are driving me a bit crazy![/color][/color]
        >[color=green]
        > >When converting from degrees to radians, you are introducing a fair
        > >amount of rounding error -- don't think for a minute you're getting the
        > >16 digit accuracy JS attempts to deliver. At best, you get, maybe,
        > >reliably 12-13, but you can push it a little at about 14 digits.[/color]
        >
        > I do not at all see why that should be, provided that an accurate value
        > for Pi is used; and Math.PI appears accurate.[/color]

        last i heard -- pi had over a billion digits and counting... you cannot
        depend on the accuracy of JS past 14 significant digits.
        [color=blue]
        >[color=green]
        > >You cannot prototype the Math object -- that feature was either
        > >overlooked or its lack was intended to dissuade people from making
        > >changes or additions to the object...howeve r, JS being what it is... you
        > >can just "glom" on whatever you want... the "this" refers to Math. All
        > >the following have been adjusted to trim the precision a couple of
        > >places (I hate it when IE says the Sin of 30 degrees is
        > >0.499999999999 9998).[/color]
        >
        > Does it? I find no numbers between 0.4999999999999 9994 and 0.5[/color]

        okay -- i was winging it --

        however:

        0.4999999999999 9990 + 0.0000000000000 0001 => 0.4999999999999 9999 (so
        there is one)

        [color=blue]
        >
        > Working in radians, except for I/O, should be quicker; however, I
        > suppose one could create a new Object called Deg with methods that use
        > Math internally.[/color]

        OP didn't want radians... most (but not all) astronomy books use
        degrees... Meeus does, and so does Duffett-Smith (if memory serves) --
        the two most likely sources for amateurs writing astronomical calculators.

        Meeus devotes an entire chapter in Astronomical Algorithms to the
        accuracy of calculators and computers... all the data he supplies has
        only 10 significant digits (in most places) or fewer. No calculation
        using more significant digits can yield any more accuracy than the data
        with which one starts.
        [color=blue]
        >
        >
        > --
        > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk / ??.Stockton@phy sics.org ©
        > Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
        > Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
        > Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)[/color]

        Comment

        • Lasse Reichstein Nielsen

          #19
          Re: degrees not radians

          Fox <dont@spam.co m> writes:
          [color=blue]
          > Lasse Reichstein Nielsen wrote:[/color]
          [color=blue]
          > I think you confuse the method 'prototype' with the abstract concept of
          > "a prototype" or the definition of the constructor function.[/color]

          There is no method called "prototype" (where I use "method" to mean
          "is a property of an object and is a function").

          Are you thinking about the "prototype" property of functions?
          [color=blue]
          > To quote Netscape documentation:
          >
          > "You can use the prototype to add properties or methods to all instances
          > of a class."[/color]

          I disagree with the way it is written. For one thing, I don't like to use
          the word "class" about a objects in a prototype base language. But, for
          now, let's use the word "class" about a constructor (i.e., a function)
          and "instance of a class" about the objects that satsify "obj instanceof
          classFunction") .
          [color=blue]
          > Oh -- and Object IS an object and it CAN be prototyped (as in, having
          > functionality added):[/color]

          The global constructor "Object" is a function, and as such an object.

          If you treat it as a constructor (a "class"), you can add properties
          to its instances by adding properties to its prototype object.
          I take it that is what you mean by "be prototyped".

          That does not add functionality to Object, but only to its instances.
          (So, I'm arguing that your way of saying it is imprecise, although
          your meaning is probably correct)

          ....[color=blue]
          > something() is automatically inherited by EVERY instantiated object (and
          > object literal) after prototyping...[/color]

          Yes. It becomes a property of the original object prototype, which is in
          the prototype chain of all language objects.
          [color=blue]
          > 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]

          I think that is merely because the objects referenced by "navigator" ,
          "history", and the DOM Nodes are host objects, which don't
          (necessarily) have the original object prototype in their prototype
          chain.

          All language objects will have "something" as an inherited method,
          also those created before.

          Try:
          ---
          function something() {alert (this + " is something!");}
          var x = new String("foo"); // or just the equivalent, var x = {};
          var d = document.create Element("div"):
          alert(typeof x.something);
          alert(typeof d.something);
          Object.prototyp e.something = something;
          alert(typeof x.something); // old object
          alert(typeof d.something); // old host object
          var x = new String("foo"){
          var d = document.create Element("div"):
          alert(typeof x.something); // new object
          alert(typeof d.something); // new host object
          ---

          In IE, the DOM div node will never have something as a property.
          In Opera 7, it does inherit from Object.prototyp e, so it seems it
          is browser dependent how host objects behave.
          [color=blue]
          > Math is a static class -- it has no constructor.[/color]

          There are no classes in a prototype based language. If we use the name
          "class" for functions that are used as constructors, then that's just
          words to make us happy. There is definitly no such thing as a "static
          class". Math is an *object*. Nothing more, nothing less. There is no
          significant difference between Math and any object with the same properties.

          ---
          var Math2 = new Object;
          for(var i in Math) {Math2[i]=Math[i];}
          ---
          That gives you a new "Math" object with exactly the same properties as
          the old one. The only difference is that some of the properties of Math
          are read-only, but it is still just an object.
          [color=blue]
          > It also has no prototype method... you cannot:[/color]

          There is no prototype "method".
          [color=blue]
          > Math.prototype. something[/color]

          No, because "Math" refers to a non-function object.
          All functions have a prototype property. Non-function objects don't.
          Nothing to do with classes.
          [color=blue]
          > which is what I meant in my original statement.[/color]

          Yes. It has no "prototype" property. Nor does Math2.
          [color=blue]
          > Both are static classes...no constructors...[I believe there are also
          > some "locks" on these, particularly history because of security concerns][/color]

          Both are objects. No more, no less (host objects, though, so they can act
          different from language objects).
          [color=blue][color=green]
          >> Because it isn't. There is no Math.prototype, because Math is not
          >> a function. It is an object.[/color]
          >[/color]
          [color=blue][color=green]
          >> 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]

          Ok, that was what I thought.
          Math is not a class (there is no such thing as a static class). SO,
          it cannot be prototyped (apart from prototyping the Object class).
          [color=blue][color=green]
          >> 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]

          I disagree. I think that is an attempt to make a non-class based language
          fit into a class based perception.
          [color=blue][color=green]
          >> 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.[/color]

          If classes are objects, yes.
          [color=blue]
          > 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)[/color]

          That is an implementation method. It is not necessarily the only one,
          but it has shown itself to be efficient.
          At the conceptual level, the methods are properties of the object
          just as fields are.
          [color=blue]
          > 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).[/color]

          In Javascript, methods are no different from other properties. No pool,
          just a reference from the object.


          Ok. The reason I don't think it is correct to associate the constructor
          to the "class" is that whether an object is an instance of a constructor
          function can change.

          Example:
          ---
          function Foo(){}
          var f = new Foo();
          Foo.prototype = new Object();
          alert(f instanceof Foo);
          ---
          This alerts "false"!

          The object in "f" was created with "new Foo()", but still it is not an
          instance of "Foo".
          The constructor doesn't define the "class"! The *prototype object* of
          the constructor, at the time the object is created, defines the class.

          Example:
          ---
          function Foo(){} // Constructor Foo
          function Bar(){} // Constructor Bar
          var f = new Foo();
          var b = new Bar();
          var oldFooProt = Foo.prototype;
          Foo.prototype = Bar.prototype;
          var fb = new Foo();
          Foo.prototype = oldFooProt;
          // now guess the result of:
          alert([f instanceof Foo,f instanceof Bar])
          alert([b instanceof Foo,b instanceof Bar])
          alert([fb instanceof Foo,fb instanceof Bar])
          ---

          Here "fb" is created from the constructor "Foo", but it is an instance
          of the (almost) unrelated constructor "Bar". Why? Because Bar's prototype
          property refers to an object that is in fb's prototype chain.


          The constructor doesn't define the class. The prototype object does.

          That is what I mean by the difference between class based and
          prototype based languages.
          In a class based language, the class (object/definition) defines the
          instances of the class.
          In a prototype based language, any object defines a "class" of the
          objects that are instances of it.

          Javascript attempts to hide this using constructor functions, but
          that's just wrapper. What really goes on below is inheritance between
          an object (the prototype object of the constructor at the time) and
          a new object. The constructor itself is just some function that is
          not related to the inheritance. It's just a language gimmick to
          enable prototype-inheritance between objects.

          The clone method I previously posted would also work in general:
          ---
          Object.prototyp e.clone = function(){
          function nop(){}; // dummy function to facilitate inheritance
          nop.prototype = this;
          return new nop();
          }
          Object.prototyp e.instanceOf = function (obj) {
          function nop(){};
          nop.prototype = obj;
          return this instanceof nop;
          }
          var rootObject = Object.prototyp e;
          ---
          Then you can write:
          ---
          var o1 = rootObject.clon e(); // new object
          var o2 = rootObject.clon e(); // new object
          var o3 = o1.clone(); // new instance of first object
          o1.foo = 42; // extend first object/prototype of third
          alert([o3.foo, o3.instanceOf(o 1), o3.instanceOf(o 2)]);
          ---

          That is pure prototype based object oriented programming (I believe it
          is closer to how it is done in the language Self). As you can see, the
          actual constructor functions are irrelevant, inheritance still works,
          instanceOf works, all because the actual prototype inheritance happens
          between the *objects*. The constructors, the "classes", are just
          scaffolds for the actual construction of objects.
          [color=blue][color=green]
          >> 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]

          Except that it makes little sense to create more than one Math object,
          since none of the "methods" operate on the object. You are correct that
          one would most likely create Math as a static class in a class based
          language, but that is because the class based language lacks a better
          way to create a namespace - which is what you really want. In Standard
          ML, it would be a module, which is just a namespace that can be opened
          or accessed. In Javascript, it is an object, which is slightly more than
          just a mapping from name to values.
          [color=blue][color=green]
          >> 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]

          Adding a property to the prototype of a number of instances saves space
          compared to adding it to each (and adds flexibility to change it later).

          Adding a property to one object is best done by adding it directly to
          the object.

          I don't think you can generalize and say that one is always better than
          the other. It depends on the actual case.
          [color=blue][color=green][color=darkred]
          >> > 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]

          And I cut the context, so I can't remember what I was replying to.
          Damn! It made sense when I wrote it.
          [color=blue]
          > 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]

          Numbers get awfully big. I too remember when 16 bit computers were a
          fantastic progress :)

          /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.'

          Comment

          • Dr John Stockton

            #20
            Re: degrees not radians

            JRS: In article <3FE58D71.A9D3C 454@spam.com>, seen in
            news:comp.lang. javascript, Fox <dont@spam.co m> posted at Sun, 21 Dec
            2003 06:09:19 :-[color=blue]
            >Dr John Stockton wrote:[color=green]
            >>
            >> JRS: In article <3FE2804E.4627B 4A8@spam.com>, seen in
            >> news:comp.lang. javascript, Fox <dont@spam.co m> posted at Thu, 18 Dec
            >> 2003 22:36:30 :-
            >>[color=darkred]
            >> >Jeff Thies wrote:
            >> >>
            >> >> Is it possible to set the Math functions to use degrees instead of
            >> >> radians?
            >> >>
            >> >> I'm working out some astronomical calculations and everything is in
            >> >> degrees. All those conversions are driving me a bit crazy![/color]
            >>[color=darkred]
            >> >When converting from degrees to radians, you are introducing a fair
            >> >amount of rounding error -- don't think for a minute you're getting the
            >> >16 digit accuracy JS attempts to deliver. At best, you get, maybe,
            >> >reliably 12-13, but you can push it a little at about 14 digits.[/color]
            >>
            >> I do not at all see why that should be, provided that an accurate value
            >> for Pi is used; and Math.PI appears accurate.[/color]
            >
            >last i heard -- pi had over a billion digits and counting... you cannot
            >depend on the accuracy of JS past 14 significant digits.[/color]


            Pi has as many digits as one cares to calculate.

            But I took your words to mean that conversion to radians introduces an
            exceptional amount of error, more than for other simple operations.

            Since basic astronomical angular data is probably no more accurate than
            *about* a micro-radian, ISTM that a numerical accuracy of even 1 in 10^9
            should be sufficient, if the algorithms are expressed prudently -
            avoiding unnecessary subtraction of nearly equal numbers, for example.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • Dr John Stockton

              #21
              Re: degrees not radians

              JRS: In article <3FE58CCB.C5862 62B@spam.com>, seen in
              news:comp.lang. javascript, Fox <dont@spam.co m> posted at Sun, 21 Dec
              2003 06:06:33 :-[color=blue]
              >[color=green]
              >>
              >> Please give a proper name - we are accustomed to seeing Fox Mahoney
              >> here, and posting as Fox can cause confusion.
              >>[/color]
              >
              >it's not like you know me personally...wh at difference does it make?[/color]


              Because it is nice not to wonder whether you and Fox Mahoney are one
              person or two persons. Should the present manifestation inherit the
              reputation of the previous one, or not?

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
              Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
              Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
              No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

              Comment

              Working...