Results 1 to 5 of 5

Thread: "toString" returns a String - Possible for more classes?

  1. #1

    "toString" returns a String - Possible for more classes?

    It seems as thought whenever you try to treat a class as a String (such as putting it inside of trace() or similar), if that class has a "toString" function, it will get the string from whatever that function returns, and will otherwise return something along the lines of "[object MyClass]".

    Are there any more of these special functions?


    Also, a bit of a far stretch, is there any way to do this with custom classes, for instance, your class may be a PolarPoint, but people can call "toLinearPoint()" and get an instance of a class which is a LinarPoint but with the same data.

    Now, this is simple enough to do manually, but let's say you pass in the PolarPoint in a function where a LinearPoint is expected. Is it possible to make Flash automatically run the toLinearPoint() function if found? Similar to how Flash will run toString automatically if passed in as a String somewhere.


    That second part is probably more of a hassle than it is worth though, but it would be REALLY handy if you are unable to extend two classes at once, but want your class to be able to be treated as both.
    Blog article of the month: Why My One Line 'if' Statements Are Unusual
    Twitter: @IQAndreas
    GitHub: IQAndreas

  2. #2
    I'm guessing what you're talking about is akin to the C# intrinsic operators? http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx

    I don't know of anything like that in ActionScript, but I hope there is!

  3. #3
    Kinda sounds like you are talking about the clone() method..

  4. #4
    I don't see what's special about trace. Yes, it calls toString, but it just does that from within. You can do that yourself and it's not special, you know?:

    Code:
    function f(...args):void {
         var i:int;
         for(var i:int = args.length; i >= 0; i++){
              trace(args[i].toString());
         }
    }
    There's nothing in AS3 that's like the implicit operators that FizixMan mentioned (except it is done to a limited extent for primitive types, like you can add a String and a Number). Yep, it's too bad.

  5. #5
    There are two. toString() and valueOf(). Can you arbitrarily make your own? No. Those that exist are defined in Object and core to the language.
    Code:
    var a = {valueOf:function(){ return 1; }};
    var b = {valueOf:function(){ return 2; }};
    trace(a + 1); // 2
    trace(b + 1); // 3

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012