簡體   English   中英

在ExtJS中銷毀一個組件后,對引用變量的處理是什么?

[英]What happens to the reference variable after a Component is destroyed in ExtJS?

ExtJS的新功能。 我有以下代碼。

var comp;
console.log('comp is - ' + comp);
comp = Ext.create('Ext.Component', {
    listeners : {
        beforedestroy : function(comp) {
            console.log('beforedestroy .. ');
        },
        destroy : function(comp) {
            console.log('destroy .. ');
        }
    }
});
console.log('comp is - ' + comp);
console.log('comp id is - ' + comp.getId());
comp.destroy();
console.log('comp is - ' + comp);
console.log('comp id is - ' + comp.getId());

chrome的控制台輸出是

comp is - undefined
comp is - [object Object]
comp id is - component-1009
beforedestroy .. 
destroy .. 
comp is - [object Object]
comp id is - component-1009

看起來即使在銷毀組件之后,該變量仍保留着對原始組件的引用。 我期望變量銷毀后具有未定義或null的值。 這是正常行為嗎?

Ext-JS不可能使您對現有組件的引用無效,因為JavaScript沒有類似C ++的引用。

例如:

var a = {b:2};
var b = a;  
// a and b both reference `{b:2}`, there is nothing you can do 
// to `{b:2}` that would change a or b to be null

您可以使用未記錄的isDestroyed屬性檢查組件是否被銷毀。

comp.isDestroyed // true

您還將注意到該組件已從注冊表中刪除

Ext.getCmp(comp.id); // undefined

您看到的行為與文檔所聲稱的一致。

嘗試通過刪除所有事件偵聽器,將它們從DOM中刪除(如果適用)並調用其destroy函數(如果可用)來破壞傳遞給它的任何對象。 此方法主要用於Ext.Element和Ext.Component類型的參數,但可以傳入Ext.util.Observable的任何子類。可以在單個調用中將任意數量的元素和/或組件傳遞給此函數,如下所示:單獨的參數。

來源-http://docs.sencha.com/extjs/4.2.1/#!/api/Ext - method - destroy

如果您查看源代碼,它不會刪除任何地方的實際組件。 也就是說,該組件在調用銷毀后實際上將是無用的,但這與被垃圾回收的對象不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM