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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard A. DeVenezia

    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

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


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

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


    JW



    Comment

    • Richard A. DeVenezia

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

      "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
      news:3f588969$0 $28902$1b62eedf @news.euronet.n l...[color=blue]
      >
      > "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> schreef in bericht
      > news:bja11p$h0b gl$1@ID-168040.news.uni-berlin.de...[color=green]
      > >
      > > function foo ( name, value ) {
      > > **something** [name] = value
      > > }
      > > foo ('X',9)
      > > alert (foo.X) // want 9
      > >
      > > What would **something** be ?[/color]
      >
      > function foo ( name, value ) {
      > foo[name] = value
      > }
      >
      >
      > JW
      >
      >[/color]


      I found what I was looking for.

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

      presumed to be the same as
      [color=blue]
      > function foo ( name, value ) {
      > foo[name] = value
      > }[/color]

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

      --
      Richard A. DeVenezia


      Comment

      • Douglas Crockford

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

        > I can add a property to a function while in the function[color=blue]
        >
        > function foo() {
        > foo.X = 1;
        > }[/color]

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

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



        Comment

        • Lasse Reichstein Nielsen

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

          "Douglas Crockford" <nospam@laserli nk.net> writes:
          [color=blue]
          > Ideally yes, but IE has a bug which frustrates that pattern,
          > requiring instead[/color]

          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.'

          Comment

          Working...