Subj : JavaScript Variable Scoping and Execution Contexts To : netscape.public.mozilla.jseng From : Greg Swindle Date : Sat Sep 04 2004 01:11 pm Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() { var objectName = "ParentObject"; this.getObjectName = function() { return objectName; } this.setObjectName = function( newObjectName ) { objectName = newObjectName; } } var ChildObject = function() { ParentObject.apply(this, arguments); this.tryToReferenceObjectNameVar = function() { var text = null; // Attempt to reference the variable "objectName", // which was created in ParentObject. try { text = "ChildObject's \"objectName\"" + " variable is \"" + objectName + "\""; } catch( error ) { alert( "Error: " + error.name + " at " + "line: " + error.lineNumber + "\n" + "\"" + error.message + "\"\n" + "Stack:\n" + error.stack + "\n" ); } return text; } } var child = new ChildObject(); child.tryToReferenceObjectNameVar(); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The last call throws this ReferenceError: Error: objectName is not defined Is this because prototyping, which I have done by calling the "apply" method of a constructor, creates an independent execution context that cannot be accessed from the "child" object's execution context? In other words, when I call child.getObjectName(); child.tryToReferenceObjectNameVar(); are two (or several) different execution contexts being referenced? How is the JavaScript interpreter handling this? TIA, Greg .