...

View Full Version : passing "this" as argument



footballer27
01-05-2012, 08:29 PM
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

felgall
01-05-2012, 08:45 PM
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.

footballer27
01-05-2012, 09:16 PM
what i have is



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
}

Old Pedant
01-05-2012, 10:02 PM
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.



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?

footballer27
01-05-2012, 10:11 PM
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.




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?

Old Pedant
01-05-2012, 10:40 PM
It works fine for me. Look:


<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>

Old Pedant
01-05-2012, 10:44 PM
Maybe you should actually remove the array element, instead of nulling it?



<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>

footballer27
01-05-2012, 11:02 PM
thanks, for some reason I was having trouble with it. It seems to work fine now



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum