Object-Oriented Programming with JavaScript, Part II: Methods: Classifying Scopes - Doc JavaScript | WebReference

Object-Oriented Programming with JavaScript, Part II: Methods: Classifying Scopes - Doc JavaScript


Object-Oriented Programming with JavaScript, Part II: Methods

Classifying Scopes

The scope of global code includes two objects: the global object and the variable object. When we say that these objects are the scope of global code, we mean that the browser can access properties of these two objects only. Some of them are real properties and some are methods. The global object is the browser window object, the one you query the location of by window.location(). The variable object holds all global variables and methods. A new object is created on the entry point to the global code, and is destroyed upon exit. The this object is the browser window object. Instead of window.location, for example, you can write this.location and get the same URL of the current page. Try it.

Eval code is code passed to the eval() function. For example, see how eval(alert("Hello there")) looks like. The scope of eval code is identical to the scope of the calling code. It includes two objects: the global object and the variable object. When we say that these objects are the scope of eval code, we mean that the browser can access properties of these two objects only. Some of them are real properties and some are methods. The global object is the browser window object, the one you query the location of by window.location(). The variable object holds all global variables and methods of the calling code. A new object is created on the entry point to the global code, and is destroyed upon exit. The this object is the same as the this object of the calling code, which is the browser window object.

Function code is code in the body of a function. The scope of function code is not trivial. It includes:

A new variable object is created and initially has only the declared arguments of the function. Normally, the this object is the global object (the browser window object). The most obvious exception is when the function is a method of an object. In this case, the this value is the object that it is a method of. The function ObjectConstructor() below is an object constructor. We create a new object, newObject, with this constructor. Inside the constructor, the value of this.location is now undefined because this is the object newObject and not the browser window object. The object newObject does not have a property by the name of location. Try it. Here are the functions that create newObject and alert this.location:

function ObjectConstructor() {
  alert(this.location);
}
function createObj() {
  var newObject = new ObjectConstructor();
}

Next: How to to ensure successful object construction


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: March 26, 2001
Revised: March 26, 2001

URL: https://www.webreference.com/js/column80/6.html