2010-09-20 41 views
5

this.remove()不是函數。怎麼來的?變量作用域:this.remove不是函數

var vehicle = function() { 
    return { 
     init: function() { 
      jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { 
       e.preventDefault(); 
       this.remove(); 
      }); 
     }, 
     remove: function() { 
      alert('test'); 
     } 
    } 
}(); 

jQuery().ready(vehicle.init); 

對不起,我感到困惑。我試圖調用我自己的「刪除」功能。這只是一個管理我的頁面上的車輛的課程。這是它的開始,它將比只是init/remove有更多的功能。

+2

您是否想調用jQuery的remove方法來移除DOM元素或提醒'test'的自定義事件? – Harmen 2010-09-20 18:33:26

回答

0

既然你說你想打電話給你自己remove功能,這裏是如何做到這一點:

var vehicle = (function() { 
    return { 
     init: function() { 
      var that = this; // step one 
      jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { 
       e.preventDefault(); 
       that.remove(); 
      }); 
     }, 
     remove: function() { 
      alert('test'); 
     } 
    } 
}()); // step zero - wrap the immediate invocation in parens 

jQuery(function() { 
    vehicle.init(); // step two 
); 
+0

它的工作原理,謝謝! – Webnet 2010-09-20 19:08:24

+1

它沒有包裝在()中,所以爲什麼呢? – Webnet 2010-09-20 19:21:29

+0

這與@ Pointy的回答有什麼不同嗎?另外,爲什麼將函數包裝在'()'中?如果你只是將返回值賦給一個變量,它將以任何方式工作。 – user113716 2010-09-20 19:24:54

5

this是一個DOM元素。要使用jQuery的.remove()方法,您需要將它包裝在一個jQuery對象中。

$(this).remove(); 

編輯:如果你希望來調用vehicle對象remove()函數,然後調用:

vehicle.remove(); 

另外,如果你希望縮短.ready()電話,你可以這樣做:

jQuery(vehicle.init); 

jQuery 1.4 release notes

jQuery().ready()技術仍然工作在1.4,但它已被棄用。請使用jQuery(document).ready()jQuery(function(){})

+0

嗯,我不認爲這是問題 - 他想在那裏的對象字面值中調用「刪除」函數。 – Pointy 2010-09-20 18:33:20

+0

我試圖調用我自己的刪除函數 – Webnet 2010-09-20 18:56:31

+1

@Webnet - 如果需要,你可以直接調用'vehicle.remove',或者你可以使用@ Pointy的答案在'init'函數中保留'this'的值,但無論如何,在事件處理程序中,this將引用接收事件的DOM元素。 – user113716 2010-09-20 19:00:40

0
var vehicle = function() { 
    return { 
     init: function() { 
      var self = this; 
      jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { 
       e.preventDefault(); 
       self.remove(); 
      }); 
     }, 
     remove: function() { 
      alert('test'); 
     } 
    } 
}(); 

jQuery().ready(function() { 
    vehicle.init(); 
}); 
+0

我認爲這不會起作用。 – Pointy 2010-09-20 18:34:48

+0

當然這不起作用,'self'指向init函數 – Harmen 2010-09-20 18:36:09

+1

不,@Harmen,「self」不會指向「init」函數,它會指向該函數的「this」值叫做。這裏的問題是「準備就緒」的設置仍然是錯誤的。 – Pointy 2010-09-20 18:39:55

2

注意 - 我們都有點困惑,因爲目前還不清楚其中「刪除」你要調用函數。

問題是你傳入了對「init」函數的引用,但是當它被稱爲「this」變量時,將引用窗口對象,而不是「vehicle」的值。爲什麼?因爲在Javascript中,「this」值僅取決於函數的調用方式。兩個函數被定義在同一個對象中的事實與它完全無關。

儘量不要做這樣的:

jQuery(function() { 
    vehicle.init(); 
}); 

當你調用「初始化」功能,通過明確地引用它作爲「車輛」的屬性的方式—對象—然後用JavaScript綁定「本」的「車輛」的價值。

編輯哦,等等我只注意到你將不得不修改你的「初始化」功能,因爲這裏面的代碼「點擊」處理器將是jQuery的通過以這樣的方式被稱爲把這個「這個」綁定到受影響的元素。因此,如果你想保持「車輛」的參考,你應該這樣做:

init: function() { 
     var originalThis = this; 
     jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { 
      e.preventDefault(); 
      originalThis.remove(); 
     }); 
    }, 
+0

尖尖 - 我認爲這是不對的。 'vehicle'是一個自我執行的函數,它返回一個具有屬性'init'的對象,這是一個函數,所以'jQuery().ready(vehicle.init);'將引用傳遞給init函數在'ready()'中使用。 – user113716 2010-09-20 18:38:59

+1

是的,我看到@patrick,但我認爲(當然猜測)他想要的是在同一個對象中調用「remove」函數,而不是jQuery「remove」。 – Pointy 2010-09-20 18:40:43

+1

'this'綁定到元素,而不是窗口,因爲它在用於事件偵聽器的函數中使用。他必須結合這一點和I.devries解決方案才能發揮作用。 – 2010-09-20 18:41:44

2

也許你正在尋找類似的東西?

var vehicle = new function() { 

    var self = this; 

    this.init = function() { 
    jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { 
     e.preventDefault(); 
     self.remove(); 
    }); 
    }; 

    this.remove = function() { 
    alert('test'); 
    }; 

}; 

...或者像這樣或許?很難說出你要做什麼...

var vehicle = new function() { 

    function remove() { 
    alert('test'); 
    } 

    this.init = function() { 
    jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { 
     e.preventDefault(); 
     remove.call(this); 
    }); 
    }; 

}; 
+0

爲什麼'新功能'? – Harmen 2010-09-20 18:41:03

+1

所以它內部的'this'將引用'new'構造的對象。 – 2010-09-20 18:44:31

+0

不要使用var foo = new function(){...};'。使用'變種富=函數(){...};'或'只是函數foo(){...};' – 2010-09-20 18:52:14

0

當調用一個函數的方法「這個」指的是物體調用它。在jQuery中,傳遞的函數作爲html元素的一個方法被調用,所以「this」成爲元素。

爲了確保您引用了正確的對象,您需要創建對原始對象的引用。

var vehicle = function() { 
     var that = { 
     init: function() { 
      jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) { 
       e.preventDefault(); 
       that.remove(); 
      }); 
     }, 
     remove: function() { 
      alert('test'); 
     } 
    } 
    return that; 
    }(); 

jQuery().ready(vehicle.init); 
相關問題