Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Another important concept is that a property belongs to an object, and only to one object. That is, the same location in the memory cannot be associated with two different objects. A property must always be associated with an object. Therefore, the following statements are legal:

var d = a.b.d
document.write(d) // prints 16
var e = a.b.e
document.write(e) // prints 42
var f = a.c.f
document.write(f) // prints true
var g = a.c.g
document.write(g) // prints king
var h = a.c.h
document.write(h) // prints 13
var i = a.c.i
document.write(i) // prints 10

As you can see, a variable may be named exactly like a property of an object. This is possible because properties are not at the same scope as variables. However, a variable cannot have the same name as an object at the same level. For example, the statement var a = a.b.d is not valid (actually it is valid, but the value of a is lost). The main object is at the same scope of the variable, because they are not properties of any other object. As a matter of fact, an object is a variable, and two variables that have the exact same name are associated with the same location in memory. The statement var a = a.b.d converts a to a simple variable containing the value 16, and not an object. It is generally a good practice to avoid naming variables by an object’s property or method, at least until you feel comfortable with objects and properties.

The output of the statement document.write(a), might be “[object create],” because that is the object’s string equivalent.

Methods

During execution of a JavaScript script, an object may invoke one or more methods to accomplish a task. As you know, objects consist of both data (properties) and functions that handle the data. These functions are called methods. Methods enable an object to perform different actions, mostly on its own properties. In Figure 6-1, all methods of the object course were located on the right-hand side of the illustration.

Methods are the analogous concept for functions in object-oriented languages. However, they differ from functions in that they have exclusive access to the state of the object, because the object’s fields (properties) are in the same scope as its methods.

Most advantages of OOP (object-oriented programming) are associated with methods. JavaScript does not completely support external libraries (other than the SRC attribute of the <SCRIPT> tag), so the following features apply mostly to built-in objects:

  • Because an object encapsulates related data and functions (methods) into a single cohesive unit, it is easy to perform maintenance activities.
  • Every language paradigm influences its application design. Therefore, scripts developed in JavaScript often make use of objects. Although you can design your scripts in a traditional procedural way, you will find it a great deal easier to use objects and methods. You will notice the difference between the traditional style and the OO style as soon as you start writing complex scripts.

JavaScript’s implementation of objects is not as powerful as that of Java, so some OO programming advantages that apply to Java do not apply to JavaScript.

Syntax

A method can exist at different levels of an object’s hierarchy. You can invoke a method using the same syntax you use to reference a property, i.e., the “dot” syntax. Methods are actually functions associated with objects. Therefore, they resemble functions in many aspects. A method is called in the following fashion:

objectReference.methodName([arguments])

objectReference is the name of the object, or any other reference. methodName is the name of the method, and arguments are the arguments that the method accepts.

Because a method is a function, the name of the method is always followed by a pair of parentheses. This rule applies also to methods that do not accept arguments.

You probably find this syntax familiar. We have been using document.write([expression]) to print HTML expressions to the page. write() is a method belonging to the built-in document object.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us