作者senser (彷彿曾經一起死過)
看板Ajax
標題Re: [問題] 如何取得父層函式的this
時間Sun Jul 17 02:58:05 2011
當然不一樣瞜..
這個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)
1F:→ senser:一般來說 這種function declaration在js要盡量避免才是.. 07/17 03:39
2F:推 amosa:function.apply( instance , args ) 07/19 13:54