※ 引述《Senkousha ( )》之銘言:
: 這是因為 "this" 實在是太常用了, 所以時常可以省略不寫.
: 但其實每當我們在 class 中宣告一個非 static 的東西,
: 而在 function 當中存取它的值的時候, 就不知不覺地用到了 "this":
: class SomeDummyClass {
: public var myInt : int = 8;
: public function getMyInt() : int {
: return myInt; // 等同於 return this.myInt;
: }
: }
看到this,忽然想要補充一個題外話
在function中,若有跟property同名的區域變數或者函式參數
可以使用this來分辨存取的變數
假如有個class
class MyClass {
private var num:Number;
public function MyClass(num:Number) {
}
}
constructor的參數與property兩者皆名為num
若要將參數num的值assign給property num
那麼就可以用this.num來將propery與參數區別開來
public function MyClass(num:Number) {
this.num = num;
}
以上:)
--
CJ Cat = Croa'J Cat = Cockroach Cat = 西街凱特 = 蜚蠊貓 = 蟑螂貓
Blog http://cjcat.blogspot.com
Gallery http://cjcat2266.deviantart.com
ptt2 Board CJWorkshop - 阿多比閃光(Adobe Flash)研討區
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.168.23.208