I am developing a firefox extension, and I think that now I have gotten into a major misunderstanding regarding Javascript, with the concept of 'prototype' to be exact. Consider the following minimal example: note the differences when setting this.demo and this.test variables:
var Example = new Array(); Example.Foo = function() { this.test = null; this.demo = "World"; }; Example.Foo.prototype = { initialize : function(resource) { this.test = "Hello"; this.display(); }, display : function() { alert(this.test + " " + this.demo); }, } window.addEventListener( "load", function() { window.obj = new Example.Foo(); obj.initialize(); }, false );
When I open Firefox, I get the expected result:
Hello World
This always works so well as long as the call () 'inside' of the js source file is displayed. However, when I call display () via the appropriate click from a menu entry, for example:
... <menupopup id="menu_ToolsPopup"> <menuitem label="Example" oncommand="obj.display();"/> </menupopup> ...
I get:
null World
initialize () was called beforehand, of course.
I am still new to Javascript and am working with existing code. Therefore, I am very confused by the current behavior. What are the best working solutions?
Christian
source share