www.webdeveloper.com
Results 1 to 7 of 7

Thread: Math.prototype - extending library

  1. #1
    Join Date
    Dec 2005
    Posts
    12

    Math.prototype - extending library

    Has anyone found a way to extend the Math object in JavaScript?

    I was wondering whether I could do this without mirroring it into a new object. I'm hoping to extend the Math package somewhat for future development.

    I tried something like this:

    Math.prototype.myMethod = function()
    {
    return 'My Method';
    }

    But Firefox 1.5 tells me 'Math.prototype has no properties.'

    Is this package locked out for developers? If so, why? Can any MSIE/Opera/Safari users relate their experiences trying to extend the Math package?

    SOLVED
    This problem has been solved. Please see comment below ("SOLUTION...") for details on how to implement the solution in your own code.

    Many thanks.
    Last edited by schephais; 12-10-2005 at 03:21 PM.

  2. #2
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    there is no Math() constructor so you cannot prototype it

    the math functions are only functions, not methods of an object

  3. #3
    Join Date
    Dec 2005
    Posts
    12
    Thanks for the quick reply, CrazyMerlin, that would explain a lot. I was misled by MozDev's definition of Math as a JavaScript object:

    A built-in object that has properties and methods for mathematical constants and functions.
    http://developer.mozilla.org/en/docs...l_Objects:Math

    I tried declaring "Math();" in a blank HTML page in Firefox and sure enough, "Math is not a function."

    The dream of an ideal solution having been dashed to pieces, is there any way to create a constructor to wrap around the Math object, construct a new object using Math as a constructor or mirror the properties and methods of the Math object into a new object?

    Part of the reason I ask is that I was hoping to increase the accuracy of some of Math's properties and methods for technical use. Also... because it's cool...

    Thanks again.

  4. #4
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    I don't think you'd be able to really.

    The Math() object doesn't define a class of object like Date does.

    The best I can suggest at the moment is either to create your own library and call it MMath(), and find out if it is intended to be a constructor in the next Core Javascript version.

  5. #5
    Join Date
    Dec 2005
    Posts
    12
    Thanks again, CrazyMerlin. I'm following your advice and creating my own library.

  6. #6
    Join Date
    Dec 2005
    Posts
    12
    SOLUTION

    I solved this problem through closer analysis of the MozDev definition of the Math object: the Math object is a LITERAL, meaning it was initialized (using the JavaScript 1.2+ initialization method) rather than constructed and then instantiated. Because of this, Math (as CrazyMerlin pointed out) has no constructor: it wasn't constructed, it was initialized!

    So, to append a method to the Math object, simply:
    Code:
    Math.myMethod = function()
    {
    return 'My Method';
    }
    Pretty slick, eh?
    Last edited by schephais; 12-10-2005 at 03:21 PM.

  7. #7
    Join Date
    Jan 2006
    Posts
    3
    i dont suppose in your own library you've found a way to rotate an image by a given angle have you. can you do other maths functions like enlarge?????

    vish

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles