Getting a method's name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken Kast

    Getting a method's name

    Here's my situation. I have an object
    function Obj () {
    this.foo = null;
    }

    a function
    function bar() {...}

    another function

    function doSomething () {
    var obj = new Obj();
    obj.foo = bar;
    doSomethingElse (obj);
    }

    In function doSomethingElse I want to create the following line of DHTML

    <div onClick="obj.fo o(); return true;">
    What I want is for onClick to be defined to be the execution of the foo
    method of obj.

    I tried document.writel n ("<div onClick='" + obj.foo + "(); return
    true;'>");
    What I get is the source code for bar stuck into the middle of the string.
    So how do I get just the name of the function bar in the string? Or
    ultimately, how do I get the handler to be bar in the DHTML?

    Thanks.

    Ken


  • Lee

    #2
    Re: Getting a method's name

    Ken Kast said:
    [color=blue]
    >In function doSomethingElse I want to create the following line of DHTML
    >
    ><div onClick="obj.fo o(); return true;">
    >What I want is for onClick to be defined to be the execution of the foo
    >method of obj.
    >
    >I tried document.writel n ("<div onClick='" + obj.foo + "(); return
    >true;'>");[/color]

    There's no trick to it. It's just a string;

    document.writel n('<div onclick="obj.fo o();return true;">');

    [color=blue]
    >What I get is the source code for bar stuck into the middle of the string.
    >So how do I get just the name of the function bar in the string? Or
    >ultimately, how do I get the handler to be bar in the DHTML?[/color]

    document.getEle mentById("theId AttributeOfYour Div").onclick=o bj.foo;

    Comment

    • Ken Kast

      #3
      Re: Getting a method's name

      But obj.foo won't be defined when that line of HTML is parsed. I
      essentially need to convert the "relative" (to the script) ref to obj.foo to
      an "absolute" ref to bar when the browser sees the HTML coming in via the
      writeln statement.

      Ken
      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
      news:cb4h360vc3 @drn.newsguy.co m...[color=blue]
      > Ken Kast said:
      >[color=green]
      > >In function doSomethingElse I want to create the following line of DHTML
      > >
      > ><div onClick="obj.fo o(); return true;">
      > >What I want is for onClick to be defined to be the execution of the foo
      > >method of obj.
      > >
      > >I tried document.writel n ("<div onClick='" + obj.foo + "(); return
      > >true;'>");[/color]
      >
      > There's no trick to it. It's just a string;
      >
      > document.writel n('<div onclick="obj.fo o();return true;">');
      >
      >[color=green]
      > >What I get is the source code for bar stuck into the middle of the[/color][/color]
      string.[color=blue][color=green]
      > >So how do I get just the name of the function bar in the string? Or
      > >ultimately, how do I get the handler to be bar in the DHTML?[/color]
      >
      > document.getEle mentById("theId AttributeOfYour Div").onclick=o bj.foo;
      >[/color]


      Comment

      • Lee

        #4
        Re: Getting a method's name

        Ken Kast said:[color=blue]
        >
        >But obj.foo won't be defined when that line of HTML is parsed. I
        >essentially need to convert the "relative" (to the script) ref to obj.foo to
        >an "absolute" ref to bar when the browser sees the HTML coming in via the
        >writeln statement.[/color]

        Then the correct solution depends on what you're actually
        trying to accomplish, and probably involves either global
        variables.

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: Getting a method's name

          "Ken Kast" <ken@NOSPAMkenk ast.com> writes:

          ....[color=blue]
          > function doSomething () {
          > var obj = new Obj();
          > obj.foo = bar;
          > doSomethingElse (obj);
          > }
          >
          > In function doSomethingElse I want to create the following line of DHTML
          >
          > <div onClick="obj.fo o(); return true;">
          > What I want is for onClick to be defined to be the execution of the foo
          > method of obj.[/color]

          Then this won't work.

          At this point of the execution, the only reference to the object is
          the first parameter of the doSomethingElse function.

          What you want to write is a way to reference that value from the global
          scope (where the intrinsic event handler is executed).

          To do that, you must make a globally available reference to the object.
          Say:
          window.xyzzy = arg1; // or whatever the parameter is called
          document.writel n ("<div onclick='window .xyzzy(); " +
          "return true;'>");
          [color=blue]
          > I tried document.writel n ("<div onClick='" + obj.foo + "(); return
          > true;'>");
          > What I get is the source code for bar stuck into the middle of the string.[/color]

          Yes. I am not sure you realize that you are building syntax here. There
          is no way for the syntax to carry an actual reference to the object. At
          best it can contain syntax that evaluates to such a reference, but only
          if such a reference is available at all in the scope where the code is
          executed.
          [color=blue]
          > So how do I get just the name of the function bar in the string?[/color]

          You don't want the name (it's "bar"). You want a Javascript expression
          (syntax) that, when evaluated as an intrinsic event handler, evaluates
          to a reference to the existing object.
          [color=blue]
          > Or ultimately, how do I get the handler to be bar in the DHTML?[/color]

          Make a global reference with either a fixed or a computed name,
          and then use that in the syntax you write.
          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Mike Foster

            #6
            Re: Getting a method's name

            Ken Kast wrote:[color=blue]
            > Here's my situation. I have an object
            > function Obj () {
            > this.foo = null;
            > }
            >
            > a function
            > function bar() {...}
            >
            > another function
            >
            > function doSomething () {
            > var obj = new Obj();
            > obj.foo = bar;
            > doSomethingElse (obj);
            > }
            >
            > In function doSomethingElse I want to create the following line of DHTML
            >
            > <div onClick="obj.fo o(); return true;">
            > What I want is for onClick to be defined to be the execution of the foo
            > method of obj.
            >
            > I tried document.writel n ("<div onClick='" + obj.foo + "(); return
            > true;'>");
            > What I get is the source code for bar stuck into the middle of the string.
            > So how do I get just the name of the function bar in the string? Or
            > ultimately, how do I get the handler to be bar in the DHTML?
            >
            > Thanks.
            >
            > Ken
            >[/color]


            Have a look at this:

            // "The following example uses a small generalised closure based function
            // that associates object instances with element event handlers."
            // http://jibbering.com/faq/faq_notes/closures.html#clObjI


            Comment

            • Ken K

              #7
              Re: Getting a method's name

              Actually, I found a simple solution by searching this group. I create
              a property bar.name="bar". In my DHTML I use obj.foo.name and it
              works hunky-dory.

              Ken

              Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<llihzv00. fsf@hotpop.com> ...[color=blue]
              > "Ken Kast" <ken@NOSPAMkenk ast.com> writes:
              >
              > ...[color=green]
              > > function doSomething () {
              > > var obj = new Obj();
              > > obj.foo = bar;
              > > doSomethingElse (obj);
              > > }
              > >
              > > In function doSomethingElse I want to create the following line of DHTML
              > >
              > > <div onClick="obj.fo o(); return true;">
              > > What I want is for onClick to be defined to be the execution of the foo
              > > method of obj.[/color]
              >
              > Then this won't work.
              >
              > At this point of the execution, the only reference to the object is
              > the first parameter of the doSomethingElse function.
              >
              > What you want to write is a way to reference that value from the global
              > scope (where the intrinsic event handler is executed).
              >
              > To do that, you must make a globally available reference to the object.
              > Say:
              > window.xyzzy = arg1; // or whatever the parameter is called
              > document.writel n ("<div onclick='window .xyzzy(); " +
              > "return true;'>");
              >[color=green]
              > > I tried document.writel n ("<div onClick='" + obj.foo + "(); return
              > > true;'>");
              > > What I get is the source code for bar stuck into the middle of the string.[/color]
              >
              > Yes. I am not sure you realize that you are building syntax here. There
              > is no way for the syntax to carry an actual reference to the object. At
              > best it can contain syntax that evaluates to such a reference, but only
              > if such a reference is available at all in the scope where the code is
              > executed.
              >[color=green]
              > > So how do I get just the name of the function bar in the string?[/color]
              >
              > You don't want the name (it's "bar"). You want a Javascript expression
              > (syntax) that, when evaluated as an intrinsic event handler, evaluates
              > to a reference to the existing object.
              >[color=green]
              > > Or ultimately, how do I get the handler to be bar in the DHTML?[/color]
              >
              > Make a global reference with either a fixed or a computed name,
              > and then use that in the syntax you write.
              > /L[/color]

              Comment

              Working...