9 Replies Latest reply on Aug 20, 2007 7:36 AM by Greg Dove

    Failed to add a new function to Number.prototype?

    lichtjiang
      To have a percentage format of a number, I add the following code:

      Number.prototype.toPercentage()=function(){
      //blahblahblah...
      }


      Then, I call it as follows:

      var somenumber:Number=33.333333;
      trace(somenumber.toPercentage());


      Check syntax gives me this error:

      There is no method with the name 'toPercentage'.

      I remember that I can add a function to a MovieClip. So, I guesss there is something wrong in my code. Any thoughts?

      Thanks!
        • 1. Re: Failed to add a new function to Number.prototype?
          kglad Adobe Community Professional & MVP
          don't type somenumber as a Number.
          • 2. Re: Failed to add a new function to Number.prototype?
            lichtjiang Level 1
            Thanks! that solved the problem. But why?
            • 3. Re: Failed to add a new function to Number.prototype?
              kglad Adobe Community Professional & MVP
              beyond my observation that typing in as2 is faulty, i have no explanation.
              • 4. Re: Failed to add a new function to Number.prototype?
                lichtjiang Level 1
                Interesting. Thanks a lot for your help!
                • 5. Re: Failed to add a new function to Number.prototype?
                  Greg Dove Level 4
                  @kglad : isn't it just compiler level checking?... its easy to change.

                  To make this work without errors:

                  Number.prototype.toPercentage=function(){
                  return String((this*100)+"%")
                  }

                  var somenumber:Number=.33333333;
                  trace(somenumber.toPercentage());

                  All you have to do is add this line to the Intrinsic class definition for Number (ie. in Number.as):

                  function toPercentage():String;

                  (alongside its already declared methods..the constructor, and methods toString and valueOf.)

                  Of course, as soon as you start doing that you kind of thing you will encounter all sorts of problems if you ever send your fla to others. But as I recall there were some native flash methods/functions that weren't declared in the intrinsic classes in flash 8 (setTimeount I think was one) that I had to fix that way and that's what the compiler checks for error reporting.
                  • 6. Re: Failed to add a new function to Number.prototype?
                    kglad Adobe Community Professional & MVP
                    messing with the flash classes is similar to messing with the system registry in windows: it's usually unwise. if there you can do something without changing the flash class files, that would be a preferable way to proceed.
                    • 7. Re: Failed to add a new function to Number.prototype?
                      Greg Dove Level 4
                      @kglad - I agree its unwise (although I think the registry is far more dangerous, lol)... I also would not use this approach... mostly because it makes your code not portable and probably breaks OOP 'rules'.... but I guess the prototype 'hack' is breaking a few OOP rules too now.
                      I was just responding to your comment where you said "i have no explanation" and not really intending it as a recommended solution. I think the reason is simply that typing in as2 is simply a compile time activity - not runtime as I believe is the case with as3. With the as2 intrinsic classes you can't change what is in the native classes, you can just change what the compiler thinks is in them - which could be a problem if you delete some of the original methods or properties listed in the intrinsic class files. It would also be a problem if you send your fla to someone else and you had 'extended' the intrinsic classes like in that way as I mentioned above.

                      In the example with setTimeout - flash 8 was originally released with the setTimeout function support in the flash player AVM, but Macromedia forgot to list it in the relevant intrinsic class file for the IDE letting the compiler know it existed... so you couldn't use it - it was an easy/legitimate fix and a useful one, but could still cause problems for others trying to use your fla if they also hadn't applied the 'patch'.
                      • 8. Re: Failed to add a new function to Number.prototype?
                        kglad Adobe Community Professional & MVP
                        then why doesn't the compiler complain about:

                        • 9. Re: Failed to add a new function to Number.prototype?
                          Greg Dove Level 4
                          Good question. When I said "I think" and it was based on limited testing for avoiding the times that the compiler complains. I hadn't realised it was that weird.

                          I think I understand now when you say its faulty - something I've never noticed before:

                          This caused three errors:
                          var a:XML=new XML();
                          a.testF()

                          var a:Array = new Array();
                          a.testF()

                          var a:Boolean = new Boolean();
                          a.testF()



                          This caused none:

                          var a:Array = new Array();
                          a.testF()

                          var a:XML=new XML();
                          a.testF()

                          var a:Boolean = new Boolean();
                          a.testF()