2010-11-20 87 views
0

這個怎麼回事—變量foo怎麼沒有從onModified()Document對象中定義?JavaScript示波器和麪向對象

function Document() { 

    var foo = "dfsadf"; 

    this.onModified = function() { 
     alert(foo); 
    }; 

} 

// Does not alert; "foo" doesn't resolve 
new Document().onModified(); 

我想對Document引用是莫名其妙私有Document變量公共方法。

+0

你確定嗎?這應該提醒''dfsadf''。 – 2010-11-20 20:46:01

+1

它適合我。 – jwueller 2010-11-20 20:46:38

+0

你正在Firefox上測試,不是嗎? :) – CMS 2010-11-20 20:48:52

回答

7

您的Document函數與DOM中的Document構造函數發生衝突。

document instanceof Document; // true 

與任何host-object其行爲完全依賴於主機環境,他們往往可以給你意想不到的效果。

據我測試過,在Firefox你是不是能夠取代它的​​價值,因此,我建議你要麼,重命名功能,或者聲明它在其他範圍