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

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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 ?
 
J

Janwillem Borleffs

Richard A. DeVenezia said:
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
 
R

Richard A. DeVenezia

Janwillem Borleffs said:
Richard A. DeVenezia said:
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.
 
L

Lasse Reichstein Nielsen

Douglas Crockford said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top