invoking a method when I have it's name as a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Fish

    #1

    invoking a method when I have it's name as a string

    Hi,

    I'm trying to figure out how to invoke a method on an object given the
    object and the method name. For example, here's a simple object with a
    method:

    function MyObj (pName, pDesc) {
    this.name = pName;
    this.desc = pDesc;
    MyObj.prototype .foo = function() {
    return this.name + " - " + this.desc;
    }
    }

    and here is some sample code to manipulate it.

    var o1 = new MyObj("nn", "dd");
    var s1 = o1.foo();

    The assignment to s1 invokes the foo method and gets back the string "nn -
    dd" as expected. now I try to find the foo method by it's name:

    var funcName = "foo";
    var theFunc = o1[funcName];
    var s2 = theFunc();

    the assignment to s2 invokes the function OK, but not in the context of the
    object. can someone tell me how to invoke a function as a method?

    Andy



  • Lasse Reichstein Nielsen

    #2
    Re: invoking a method when I have it's name as a string

    "Andy Fish" <ajfish@blueyon der.co.uk> writes:
    [color=blue]
    > The assignment to s1 invokes the foo method and gets back the string "nn -
    > dd" as expected. now I try to find the foo method by it's name:
    >
    > var funcName = "foo";
    > var theFunc = o1[funcName];
    > var s2 = theFunc();
    >
    > the assignment to s2 invokes the function OK, but not in the context of the
    > object. can someone tell me how to invoke a function as a method?[/color]

    theFunc.call(o1 )

    There are two methods of function objects that allow them to pretend to
    be methods: "call" and "apply". The only difference is in how they transfer
    the arguments to the function. "Call" takes the arguments one at a time,
    and "apply" takes them as one array (and it is a type error if the second
    argument of "apply" isn't an array).

    function myFunc(arg1,arg 2,arg3) {...}

    myFunc.call(myO bj,arg1,arg2,ar g3);
    myFunc.apply(my Obj,[arg1,arg2,arg3]);

    /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

    • Andy Fish

      #3
      Re: invoking a method when I have it's name as a string

      Thanks, I'd missed that one

      hey, this javascript stuff ain't so bad after all... :-)

      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:fzk56bcb.f sf@hotpop.com.. .[color=blue]
      >
      > theFunc.call(o1 )
      >
      > There are two methods of function objects that allow them to pretend to
      > be methods: "call" and "apply". The only difference is in how they[/color]
      transfer[color=blue]
      > the arguments to the function. "Call" takes the arguments one at a time,
      > and "apply" takes them as one array (and it is a type error if the second
      > argument of "apply" isn't an array).
      >
      > function myFunc(arg1,arg 2,arg3) {...}
      >
      > myFunc.call(myO bj,arg1,arg2,ar g3);
      > myFunc.apply(my Obj,[arg1,arg2,arg3]);
      >
      > /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.'[/color]


      Comment

      • Vjekoslav Begovic

        #4
        Re: invoking a method when I have it's name as a string

        "Andy Fish" <ajfish@blueyon der.co.uk> wrote in message
        news:t6p_a.152$ fb1.1755226@new s-text.cableinet. net...[color=blue]
        > Hi,
        >
        > I'm trying to figure out how to invoke a method on an object given the
        > object and the method name. For example, here's a simple object with a
        > method:
        >
        > function MyObj (pName, pDesc) {
        > this.name = pName;
        > this.desc = pDesc;
        > MyObj.prototype .foo = function() {
        > return this.name + " - " + this.desc;
        > }
        > }
        >
        > and here is some sample code to manipulate it.
        >
        > var o1 = new MyObj("nn", "dd");
        > var s1 = o1.foo();
        >
        > The assignment to s1 invokes the foo method and gets back the string "nn -
        > dd" as expected. now I try to find the foo method by it's name:
        >
        > var funcName = "foo";
        > var theFunc = o1[funcName];
        > var s2 = theFunc();
        >
        > the assignment to s2 invokes the function OK, but not in the context of[/color]
        the[color=blue]
        > object. can someone tell me how to invoke a function as a method?[/color]

        var funcName = "foo";
        var s2 = o1[funcName]();


        Comment

        • Martin Honnen

          #5
          Re: invoking a method when I have it's name as a string



          Andy Fish wrote:[color=blue]
          > Hi,
          >
          > I'm trying to figure out how to invoke a method on an object given the
          > object and the method name. For example, here's a simple object with a
          > method:
          >
          > function MyObj (pName, pDesc) {
          > this.name = pName;
          > this.desc = pDesc;
          > MyObj.prototype .foo = function() {
          > return this.name + " - " + this.desc;
          > }
          > }
          >
          > and here is some sample code to manipulate it.
          >
          > var o1 = new MyObj("nn", "dd");
          > var s1 = o1.foo();
          >
          > The assignment to s1 invokes the foo method and gets back the string "nn -
          > dd" as expected. now I try to find the foo method by it's name:
          >
          > var funcName = "foo";
          > var theFunc = o1[funcName];
          > var s2 = theFunc();
          >
          > the assignment to s2 invokes the function OK, but not in the context of the
          > object. can someone tell me how to invoke a function as a method?[/color]

          Once you assign the method to a variable that function is unbound thus
          don't do that, simply call the method on the object
          var s2 = o1[funcName]()



          --

          Martin Honnen
          http://JavaScript.FAQTs.com/

          Comment

          • Andy Fish

            #6
            Re: invoking a method when I have it's name as a string

            Hmm, now I'm confused again. how come this works:

            var s2 =(o1[funcName])();

            works but this doesn't:

            var theFunc = o1[funcName];
            var s2 = theFunc();

            you'll notice I even put o1[funcName] in brackets for the first example,
            which surely must force it to be evaluated as an expression which evaluates
            to a value which should be able to be stored in a variable. I even tried
            this slightly strange construct:

            var s2 =(theFunc = o1[funcName])();

            and that doesn't work either.

            "Vjekoslav Begovic" <vjbegovic@inet .hr> wrote in message
            news:bhd847$4hb $1@sunce.iskon. hr...[color=blue]
            > "Andy Fish" <ajfish@blueyon der.co.uk> wrote in message
            > news:t6p_a.152$ fb1.1755226@new s-text.cableinet. net...[color=green]
            > > Hi,
            > >
            > > I'm trying to figure out how to invoke a method on an object given the
            > > object and the method name. For example, here's a simple object with a
            > > method:
            > >
            > > function MyObj (pName, pDesc) {
            > > this.name = pName;
            > > this.desc = pDesc;
            > > MyObj.prototype .foo = function() {
            > > return this.name + " - " + this.desc;
            > > }
            > > }
            > >
            > > and here is some sample code to manipulate it.
            > >
            > > var o1 = new MyObj("nn", "dd");
            > > var s1 = o1.foo();
            > >
            > > The assignment to s1 invokes the foo method and gets back the string[/color][/color]
            "nn -[color=blue][color=green]
            > > dd" as expected. now I try to find the foo method by it's name:
            > >
            > > var funcName = "foo";
            > > var theFunc = o1[funcName];
            > > var s2 = theFunc();
            > >
            > > the assignment to s2 invokes the function OK, but not in the context of[/color]
            > the[color=green]
            > > object. can someone tell me how to invoke a function as a method?[/color]
            >
            > var funcName = "foo";
            > var s2 = o1[funcName]();
            >
            >[/color]


            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: invoking a method when I have it's name as a string

              Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
              [color=blue]
              > For the full explanation, you will have to read the ECMA 262
              > specification[1][/color]

              and the missing footnote is
              [1] <URL:http://www.ecma-international.o rg/publications/standards/ECMA-262.HTM>

              /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

              • Andy Fish

                #8
                Re: invoking a method when I have it's name as a string

                "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                news:ptj94rsz.f sf@hotpop.com.. .[color=blue]
                > "Andy Fish" <ajfish@blueyon der.co.uk> writes:
                >[color=green]
                > > Hmm, now I'm confused again. how come this works:
                > >
                > > var s2 =(o1[funcName])();
                > >
                > > works but this doesn't:
                > >
                > > var theFunc = o1[funcName];
                > > var s2 = theFunc();[/color]
                >[color=green]
                > > you'll notice I even put o1[funcName] in brackets for the first example,
                > > which surely must force it to be evaluated as an expression which[/color][/color]
                evaluates[color=blue][color=green]
                > > to a value which should be able to be stored in a variable.[/color]
                >
                > Not really. Brackets are only grouping, not meaningful
                >[/color]
                <snip>

                OK, I get it now.

                I thought that by putting brackets round it, I would force it to be a
                "normal" expression (i.e. one with only one value). It all makes sense when
                you add in the concept of reference expressions.

                I prostrate myself at the master of javascript, sorry ECMAScript.

                Andy



                Comment

                Working...