SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Javascript Object Problem

    My problem is a bit complex...so:
    I have something like this:

    Code:
    var main_class = function() {
        
        this.st = 'hello';
    }
    
    main_class.prototype.func_a = function() {
    
       alert(this.st);
    }
    
    main_class.prototype.func_b = function() {
    
       var anon = function() {
             this.func_a();
       }
    
    }
    
    var main = new main_class();
    main.func_b();
    The problem is that in func_b I have to declare an anonymous function and from that i have to call the func_a. Don't ask. This is how it is.
    The problem is that it won't call it. Because this refers to the anonymous function. Is there some keyword to go up a level and get the reference of the main object?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    main_class.prototype.func_b = function(){
    	var that=this;
    	var anon = function(){
    		that.func_a();
    	}
    	return anon();
    }
    var main = new main_class();
    main.func_b();

    I won't ask why you don't use main_class.prototype.func_b = main_class.prototype.func_a;

  3. #3
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow, man. Thanks. You saved my day!


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
  •