...

View Full Version : new function() as object



codegoboom
08-29-2004, 05:10 AM
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()".

codegoboom
08-29-2004, 06:10 AM
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? :D

err, just tell me if I'm wrong, I mean... :p

codegoboom
08-29-2004, 09:10 AM
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...

Roy Sinclair
08-30-2004, 08:25 PM
Talking to yourself the first sign of impending insanity (or a career in IS, the two are not always distinguishable) :D

codegoboom
08-31-2004, 01:22 AM
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. :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum