Re: [問題] 如何取得父層函式的this

看板Ajax作者 (彷彿曾經一起死過)時間14年前 (2011/07/17 02:58), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串5/5 (看更多)
當然不一樣瞜.. 這個function declaration 裡的this會refer到global object(就是你的window哩) 另外原作大概是筆誤寫了個匿名函式.. 得給他個名字或是包起來才不會error function myCoolFunction(){ this.hide() <---這邊的this } (function{ this.hide() <---這邊的this })() either way.. ※ 引述《windwofswold ( ◤〔ζ狼ζ〕◢)》之銘言: : 你要這種嗎XD : $('#tt tr').click(function(){ : var self = this; : function(){ : self.xxxx; // 父層的this : this.hide() : } : }); : 如果第二個function 是包callback : 他的this才會不一樣不是嗎@__@ : ※ 引述《genomini (不重要)》之銘言: : : 假如 : : $('#tt tr').click(function(){ <--父層 : : function(){ : : this.hide() <---這邊的this : : } : : }); : : 請問有無指令是可以取得父層的this呢? : : 還是說只能在外面多加一個變數存父層的this : : 然後再子層呼叫那變數@@ : : 如 : : $('#tt tr').click(function(){ <--父層 : : var x = $(this); : : function(){ : : x.hide() <---這邊的this : : } : : }); -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 71.119.252.59 ※ 編輯: senser 來自: 71.119.252.59 (07/17 03:38)

07/17 03:39, , 1F
一般來說 這種function declaration在js要盡量避免才是..
07/17 03:39, 1F

07/19 13:54, , 2F
function.apply( instance , args )
07/19 13:54, 2F
文章代碼(AID): #1E8Tz156 (Ajax)
文章代碼(AID): #1E8Tz156 (Ajax)