SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question how to find the methods name within it

    Hi,

    I am stuck with this.
    I wanted to evaluate a function within a method.

    Code:
    function myfunc(){
     this.func = function(param){
      //code
      eval('this.func1');//This will not work
     }
     this.func1 = function(param){
      //code
     }
    }
    
    myvar = new myfunc();
    myvar.func('aa');
    My question is how should i know that the template has been set in myvar within the function myfunc()

    Please help.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function myfunc(){
     this.func = function(param){
      this.func1(param);
     }
     this.func1 = function(param){
      //code
     }
    }
    
    myvar = new myfunc();
    myvar.func('aa');

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that was just an example.
    Actually I pass it to a Ajax function which onreadystate change should call this.func1 by evaluating it.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is this a good enough example?

    Code:
                function test1() {
                    alert('test1 called');
                }
                function test2(val) {
                    alert('test2 called with param: ' + val);
                }
                function test1_call(p) {
                    p();
                }
                function test2_call(p, val) {
                    p(val);
                }
                window.onload = function() {
                    test1_call(test1);
                    test2_call(test2, 'this is my parameter');
                };


  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function myfunc(){
     this.func = function(param){
      //code
      AJAX(url, 'this.func1');//The second param is a string of the name of the callback function. It will be evaluated by AJAX onreadystate.
     }
     this.func1 = function(param){
      //code
     }
    }
    
    myvar = new myfunc();
    myvar.func('aa');
    This is what i meant.
    So i just want to know the name that 'this' will be to pass on to the AJAX Function.

  6. #6
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why does it have to be a string? Why can't you just send the function literal as a parameter?

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ize View Post
    Why does it have to be a string? Why can't you just send the function literal as a parameter?
    Because the AJAX function I have evaluates a function with whatever values and variables I pass

  8. #8
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
                function test1() {
                    alert('test1 called');
                }
                function test2(val) {
                    alert('test2 called with param: ' + val);
                }
                function test1_call(p) {
                    eval(p)();
                }
                function test2_call(p, val) {
                    eval(p)(val);
                }
                window.onload = function() {
                    test1_call('test1');
                    test2_call('test2', 'this is my parameter');
                };



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •