I am just wondering when it's necessary to use th开发者_开发技巧e keyword this.somevar
instead of just somevar
.
I usually don't use this under the following circumstances:
- i have made a var private for a class
- i refer to it in order to use it as a local var for that class
correct? or is this not just a style thing?
You generally only have to use this.varname if you are inside a function that has a parameter with the same name and you want to refer to the member variable. Whether or not the member var is private does not have any effect on having to use 'this'.
As has already been mentioned, you don't need to use this in a very simple case, where you want to refer to a class member variable from a class member method, e.g.:
public function myClassMethod() : void
{
// "this" is redundant here, as it is implied if omitted
this.myPrivateOrPublicVariable = 123;
}
Do note though that there are cases where this (which is evaluated at the time it is encountered in a program, rather than at instantiation as one might guess) does not refer to the class in which it is written. More specifically, this happens when you use it inside a function that will get called using a method such as Function.apply(). This method allows the caller to specify the object that will be referred to by this, and it is commonly used in callbacks, e.g. in tweening engines.
// Call myFunction(listOfArguments[0], listOfArguments[1] ...),
// with this referring to objectReferredToByThis.
myFunction.apply(objectReferredToByThis, listOfArguments);
Here's an example with the popular tweening engine Tweener, where the default behavior is to have this refer to the tweened object (i.e. the MovieClip _mc), rather than to an instance of the class in which this is written (i.e. MyClass).
package
{
/* ... imports ... */
public class MyClass extends Sprite
{
private var _mc : MovieClip;
public function tweenSomething() : void
{
var tween : Object = {
x: 0,
time: 1,
onComplete: function() : void {
// Here, "this" will not refer to the instance
// of MyClass, but rather to the _mc MovieClip,
// which is the object being tweened below.
this.visible = false;
}
};
Tweener.addTween(_mc, tween);
}
}
}
In the above code, the use of this is necessary, because we want to explicitly instruct the runtime to modify the visible state of our MovieClip (_mc). Had we not used this, the visible reference would have referred to MyClass.visible, because of function closure.
If you use this inside an object this will refer to the object itself. Here is an example where you would use it:
package
{
public class foo
{
private var variable:uint;
public function foo(variable:uint)
{
this.variable = variable;
}
}
}
Something to add to the other answers: In ActionScript 3, you can reference member variables and functions basically two ways:
// 1. the normal way
obj.var = 5;
obj.func();
// 2. the auxiliary way
obj["var"] = 5;
obj["func"]();
This may seem pointless, but there are certain cases when, for instance, concatenating Strings into various combinations to use random, dynamically-specified variables or functions in an object can be useful, like this:
private function someFunc():void
{
obj[randomFuncStr()]();
}
private function randomFuncStr():String
{
// randomly decide on a function to call and return its name as a String
}
But what if you want to use Strings to access variables or functions inside of this
? This doesn't work:
"var" = 5; // error
"func"(); // error
So what you have to do instead is this:
this["var"] = 5; // the this keyword is good for something after all!
this["func"](); // " " " " " " " " "
Other people have already mentioned this next reason, but the first time I ever took this.var
or this.func()
seriously in AS3 (and I usually still look at its usage with clear contempt) was when I was overriding UIComponent.setLayoutBoundsSize()
, which has a parameter named width
and a parameter named height
. UIComponent also uses these names as properties, and I don't like changing parameter names when overriding a method. So saying things like this.width = x;
or this.height = y;
suddenly became relevant. Aside from these two scenarios, I see no reason to use this
when referring to a variable or function within the same object internally.
As a side note, if you're having to do this next little thing because of the way an API has been constructed or for some other really good reason (try to avoid setting things up like this yourself), occasionally you might pass this
in as an argument to a function or something that needs a reference back to the calling object.
How about to start using this
keyword, first of all, cause it helps you quickly recognize whether the variable local or global (method or class owned) in the code. All you need a piece of the code where you can see this
and then realize immediately that this is a class variable visible globally, instead of additional scrolling for the target definition.
精彩评论