Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    85
    Thanks
    13
    Thanked 1 Time in 1 Post

    passing "this" as argument

    I'm used to java so I keep wanting to us something like

    someOtherObject.someMethod(this);

    function someOtherObject = function(){}

    someOtherObject.prototype.someMethod = function(object){
    object.doSomething();
    }

    I can't seem to get it to work

    I'm not trying to pass a DOM object, I'm trying to pass a custom function object. Any help would be greatly appreciated

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Are you sure that 'this' still points to the correct object at the point where you are trying to pass it?

    There are a number of situations where JavaScript assigns a different object to this than the one you expect which is why a lot of people assign it to _this or that or self in order to make sure they have a reference to the right object.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    85
    Thanks
    13
    Thanked 1 Time in 1 Post
    what i have is
    Code:
    function bigObject(){
    this.array = {a bunch of smallObject}
    }
    
    bigObject.prototype.removeSmallObject(smallObject){
       for(var i = 0; i < array.length; i++){
          if(array[i] == smallObject)
              array[i] = null;
       }
    }
    
    function smallObject(bigObject){
        this.big = bigObject;
        bigObject.removeSmallObject(this);// This is what I would love to do, but don't know how
    }

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    I don't get it.

    Your function smallObject(bigObject) is a constructor for a smallObject.

    So if you tried to create some of those small objects as you populated the array in bigObject, you'd be trying to remove them from the array before they were put in the array.

    Code:
    function bigObject(){
        this.array = [ new smallObject(this), new smallObject(this), new smallObject(this) ];
    }
    So the constructor for smallObject is called, which causes it to then search in the arrray in the bigObject to find a reference to itself. But the reference doesn't exist *YET* because the smallObject is still being constructed. So the reference isn't found. No error. The smallObject constructor completes. And *THEN* the reference to that new smallObject is placed in the array.

    I think the root of the problem is that you are trying to do the removal from the array as part of the constructor. Shouldn't that be a separate method?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    85
    Thanks
    13
    Thanked 1 Time in 1 Post
    sorry about that, it is in a seperate method. I just didn't want to write all the code. so yes, the removeSmallObject() wont be called until the user does something that will call it.

    Code:
    function bigObject(){
    this.arr = {a bunch of smallObject}
    }
    
    bigObject.prototype.removeSmallObject = function(smallObject){
       for(var i = 0; i < this.arr.length; i++){
          if(this.arr[i] == smallObject)
              this.arr[i] = null;
       }
    }
    
    function smallObject(bigObject){
        this.big = bigObject;
    }
    
    smallObject.prototype.userDoesSomething = function(){
       //not worried about the first this just the passing of this
       this.big.removeSmallObject(this);
    }
    Is there way to this?
    Last edited by footballer27; 01-05-2012 at 10:18 PM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    It works fine for me. Look:
    Code:
    <html>
    <head>
    <script>
    
    function bigObject(){
        this.arr = [ new smallObject(this,"A"), new smallObject(this,"B"), new smallObject(this,"C") ];
        this.toString = function(){ 
           var msg = "contains " + this.arr.length + " small objects\n";
           for ( var i = 0; i < this.arr.length; ++i )
           {
              if ( this.arr[i] == null ) { msg += "--null--\n"; }
              else { msg += this.arr[i].name + "\n"; }
           }
           return msg;
        };
               
    }
    
    bigObject.prototype.removeSmallObject = function(smallObject){
       for(var i = 0; i < this.arr.length; i++){
          if(this.arr[i] == smallObject)
              this.arr[i] = null;
       }
    }
    
    function smallObject(bigObject, n){
        this.big = bigObject;
        this.name = n;
    }
    
    smallObject.prototype.userDoesSomething = function(){
       //not worried about the first this just the passing of this
       this.big.removeSmallObject(this);
    }
    
    var bo = new bigObject();
    alert( bo );
    var so = bo.arr[1];
    so.userDoesSomething();
    alert( bo );
    </script>
    </head>
    <body>
    here
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    Maybe you should actually remove the array element, instead of nulling it?

    Code:
    <html>
    <head>
    <script>
    
    function bigObject(){
        this.arr = [ new smallObject(this,"A"), new smallObject(this,"B"), new smallObject(this,"C") ];
        this.toString = function(){ 
           var msg = "contains " + this.arr.length + " small objects\n";
           for ( var i = 0; i < this.arr.length; ++i )
           {
              if ( this.arr[i] == null ) { msg += "--null--\n"; }
              else { msg += this.arr[i].name + "\n"; }
           }
           return msg;
        };
               
    }
    
    bigObject.prototype.removeSmallObject = function(smallObject){
       for(var i = 0; i < this.arr.length; i++){
          if(this.arr[i] == smallObject)
          {
              this.arr.splice( i, 1 );
              return;
          }
       }
    }
    
    function smallObject(bigObject, n){
        this.big = bigObject;
        this.name = n;
    }
    
    smallObject.prototype.userDoesSomething = function(){
       //not worried about the first this just the passing of this
       this.big.removeSmallObject(this);
    }
    
    var bo = new bigObject();
    alert( bo );
    var so = bo.arr[1];
    so.userDoesSomething();
    alert( bo );
    </script>
    </head>
    <body>
    here
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    footballer27 (01-05-2012)

  • #8
    New Coder
    Join Date
    Oct 2011
    Posts
    85
    Thanks
    13
    Thanked 1 Time in 1 Post
    thanks, for some reason I was having trouble with it. It seems to work fine now


  •  

    Posting Permissions

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