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

Using the Array Notation

You can refer to properties and methods using the “dot” syntax or an array notation. In this notation, square brackets replace the dots. For example, the following expression refers to a.b.d:

a["b"]["d"]

You can use the array notation for both properties and methods. The general syntax is:

objectReference["propertyName"]
objectReference["methodName"]([arguments])

It is important to understand this alternative syntax, because you cannot always use the traditional dot syntax. For example, the first character of a property name cannot be a digit when using the dot syntax. When you create an array using the built-in Array object, you can only refer to the elements of the array via the array notation (e.g., myArray[0], myArray[99]). You must always use double quotes when you refer to a property of an object that is not an array. Here are some examples for using the array notation to reference methods and properties:

document["write"]("hello!")
window["alert"]("howdy!") // note: alert() == window.alert()
Math["PI"]

Sometimes you can only use the array notation. Suppose the variable str holds the string "write". You can use the following syntax instead of document.write():

document[str]()

However, you cannot use document.str() because that is equivalent to document["str"](). Another situation in which you should use the array notation is when you want to name a property not according to the identifier rules. For example, myObject["*"] is possible only with the array notation. When you use the array notation, the value in the square brackets should be a string, because the content is evaluated.

Object Based Versus Object Oriented


Note:  This explanation is intended mostly for programmers who are familiar with object-oriented environments. Therefore, the basic terms of object-oriented programming will not be explained in this book—they are beyond the scope of this book. Only the most basic differences between JavaScript and full object-oriented languages are introduced here. You will discover many other minor differences as you learn the language. If you are new to object structures, you should skip this explanation altogether.


JavaScript is based on a simple object-oriented paradigm. This paradigm is often called object based, as opposed to object oriented. Classes do not exist in JavaScript (all objects belong to one “class”), nor do packages (because a package groups classes together). The object hierarchy in JavaScript is a containment hierarchy, not an inheritance hierarchy as in Java and C++. That is, an object does not inherit from other objects, but it can be contained by another object if it is a property of that object. Most object-oriented languages require static resolution of objects at compile time. However, an object-oriented language may require dynamic method bindings because polymorphism allows multiple definitions of methods sharing a common name. Calling such polymorphic methods often cannot be resolved until run time. JavaScript is completely based on dynamic binding. That is, object references are checked at run time. There are many other differences between the object paradigm in JavaScript and the one in full object-oriented languages (such as Java and C++).

Summary

This chapter discussed the basics of object-based programming, based on JavaScript’s object model. Its purpose is to introduce you to general object-oriented and object-based terminology and to a bit of JavaScript syntax. Every language paradigm heavily influences the design of applications in that language. Therefore, scripts written in JavaScript are typically object-based systems. Procedural scripting is possible in JavaScript, but object-based scripting has many advantages. Because JavaScript has a large set of built-in objects, including very useful methods and properties, a basic understanding of the object structure is necessary. If you are a beginner, and find objects difficult to understand, don’t be too concerned. We promise that you will understand these concepts as we move on, mostly by studying examples.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us