Is there a way of setting the default value of a method scope parameter to be the caller?
In AS3 you can set default values f开发者_运维问答or method parameters like so:
function myFuntion(param1:String="hello",param2:int=3) {
And you can pass a reference to an object by saying:
//method of Class1
function myFuntion(obj:Object) { } //do something with obj
//in Class2
var class1:Class1 = new Class1();
class1.myFunction(this);
So the question, is there a keyword that can be used like:
//method of Class1
function myFuntion(obj:Object = CALLER) { } //do something with obj
//in Class2
var class1:Class1 = new Class1();
class1.myFunction();
The only default function parameter value that is accepted for the type Object is 'null'.
function myFunction(obj:Object = null):void {};
var class1:Class1 = new Class1();
class1.myFunction();
No, there isn't a way to what you ask, and that is a good thing for encapsulation and code readability. You should be forced to deliberately pass this so that it is clear to anyone reading Class2.as what your function is being given references to.
In general, you should ask yourself "why?" anytime you have a function parameter of type Object (that's pretty general!). I'm not saying there is never a good reason for it--for error reporting purposes, for example--but all too often it's the sign of poor OOP design (e.g. using an Object because you're too lazy to make a proper data structure class for what you're passing, or to circumvent typechecking)
精彩评论