Object-Oriented Programming . 73 Using (Submit web site) the Array Notation
Monday, April 30th, 2007Object-Oriented Programming . 73 Using the Array Notation You can refer to properties and methods using the dot syntax or an array notation. In array 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. The basic terms of object-oriented programming will not be explained, as 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 Chapter
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services