Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Add properties to function while in function with out specifying function ? (http://www.velocityreviews.com/forums/t872154-add-properties-to-function-while-in-function-with-out-specifying-function.html)

Richard A. DeVenezia 09-05-2003 12:53 PM

Add properties to function while in function with out specifying function ?
 
I can add a property to a function while in the function

function foo () {
foo.X=1;
}
alert (foo.X) // undef
foo()
alert (foo.X) //1

or

function foo (name,value) {
foo [name] = value
}
foo ('A',1)
alert (foo.A) // 1

how would I add a property to currently running function without naming the
function ?

self doesn't work
function foo (name,value) {
self [name] = value
}
foo ('A',1)
alert (foo.A)
alert (self['A'])



function foo ( name, value ) {
**something** [name] = value
}
foo ('X',9)
alert (foo.X) // want 9

What would **something** be ?

--
Richard A. DeVenezia




Janwillem Borleffs 09-05-2003 01:02 PM

Re: Add properties to function while in function with out specifying function ?
 

"Richard A. DeVenezia" <radevenz@ix.netcom.com> schreef in bericht
news:bja11p$h0bgl$1@ID-168040.news.uni-berlin.de...
>
> function foo ( name, value ) {
> **something** [name] = value
> }
> foo ('X',9)
> alert (foo.X) // want 9
>
> What would **something** be ?


function foo ( name, value ) {
foo[name] = value
}


JW




Douglas Crockford 09-05-2003 01:15 PM

Re: Add properties to function while in function with out specifying function ?
 
> I can add a property to a function while in the function
>
> function foo() {
> foo.X = 1;
> }


Ideally yes, but IE has a bug which frustrates that pattern, requiring instead

foo = function () {
foo.X = 1;
};

http://www.crockford.com/javascript/remedial.html


Richard A. DeVenezia 09-05-2003 01:17 PM

Re: Add properties to function while in function with out specifying function ?
 
"Janwillem Borleffs" <jwb@jwbfoto.demon.nl> wrote in message
news:3f588969$0$28902$1b62eedf@news.euronet.nl...
>
> "Richard A. DeVenezia" <radevenz@ix.netcom.com> schreef in bericht
> news:bja11p$h0bgl$1@ID-168040.news.uni-berlin.de...
> >
> > function foo ( name, value ) {
> > **something** [name] = value
> > }
> > foo ('X',9)
> > alert (foo.X) // want 9
> >
> > What would **something** be ?

>
> function foo ( name, value ) {
> foo[name] = value
> }
>
>
> JW
>
>



I found what I was looking for.

function foo (name,value) {
var callee = arguments.callee
callee[name] = value
}
foo ('X', 9)
alert (foo.X)

presumed to be the same as

> function foo ( name, value ) {
> foo[name] = value
> }


but not needing to know the function name at source writing time.

--
Richard A. DeVenezia



Lasse Reichstein Nielsen 09-05-2003 01:43 PM

Re: Add properties to function while in function with out specifying function ?
 
"Douglas Crockford" <nospam@laserlink.net> writes:

> Ideally yes, but IE has a bug which frustrates that pattern,
> requiring instead


Which version of IE is that, and what is the bug?
It would sound like it prevents function declarations from declaring
recursive functions.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'


All times are GMT. The time now is 08:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.