Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts

    new function() as object

    To construct an object that is invoked like "Math", where only internal methods take args and parentheses are ommitted [e.g., Math.abs(n)], wouldn't the "new" operator belong at the top?

    -------------------------------------

    var Obj = new function()
    {
    return {echo:echo};
    function echo(string){alert(string);}
    }

    Obj.echo("blurb");

    -------------------------------------


    Note that this behaves differenty than "new Function()".
    *this message will self destruct in n-seconds*

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to be more clear about what I'm asking:

    Isn't the above example effectively identical to doing any of the following?

    -------------------------------------

    function Obj()
    {
    this.echo = function(string){alert(string);}
    }

    // call...

    new Obj().echo("blurb");

    //or call...

    myObj = new Obj;
    myObj.echo("blah");

    //or construct...

    function Obj2()
    {
    return {echo:echo};
    function echo(string){alert(string);}
    }

    // and call...

    Obj2().echo("boo");

    -------------------------------------


    The differences appear to be insignificant... who knows?
    ... or cares to know, besides me?
    err, just tell me if I'm wrong, I mean...
    Last edited by codegoboom; 08-29-2004 at 08:12 AM. Reason: That'll do it.
    *this message will self destruct in n-seconds*

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, now i see what that's about... the function becomes the object that it returns, immediately, so there's really no need to wrap it that way, as this is no different:

    var Obj =
    {
    method:
    function(arg){// do something with arg...}
    };

    Obj.method(arg);


    alrighty then...
    Last edited by codegoboom; 08-30-2004 at 07:00 AM. Reason: Note to self.
    *this message will self destruct in n-seconds*

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Talking to yourself the first sign of impending insanity (or a career in IS, the two are not always distinguishable)
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh well; after the fog cleared, I ended up knowing how to put private variables into a literal object syntax... just more nonsense, though, I suppose.
    *this message will self destruct in n-seconds*


  •  

    Posting Permissions

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