
|
|
|||
JavaScript ObjectsBeyond arrays, the complex variable type that JavaScript provides for managing associated data is the object. Just as an array is a variable of variables, an object can be thought of as a variable of both variables and functions. An object serves as a container for a number of variables and functions, which are usually related in purpose. Variables and functions in objects are called properties and methods respectively. Object syntaxWe will first consider working with existing objects. You will most likely not be creating your own objects, but rather working with the inbuilt ones, so we will start with syntax for accessing objects. As an object is a variable, it is treated similarly in syntax. However, to refer to properties and methods within an object, we use dot notation. Dot notation involves, as you may have guessed, dots. // ourobject is an object containing a property sometext, The syntax is very simple: where an array had the identifier of the element in square brackets, objects have the identifier placed after a dot. Inbuilt JavaScript ObjectsObjects are easier to understand when we consider the inbuilt
JavaScript objects. You may have recognised that dot in our previous
example from the JavaScript has a number of useful inbuilt objects, including Date, Math, String, Form, Image and more. Creating Objects: SimpleAn object can be created very simply, similarly to an array: person = Object(); We use the global Next, we create the function - well, method - Finally, just as we encountered function scope in JavaScript Functions, objects have object scope. Because of the object scope within an object, we refer to the object via Creating Objects: AdvancedObjects are part of a programming approach known as object oriented programming, or OOP. A fundamental concept of OOP is polymorphism, or many forms, and this applies directly to objects through classes. Classes involves taking a step back from objects, as classes define the type and nature of an object. Consider, for example, cars. A car is a type of object. If I had a Fiat car, for example, it would be an actual object, one of the class of objects with the type "car". In programming, we say that my Fiat is an instance of the class Car. We also have to define the class Car, and create the instance for my Fiat. Consider this:
This is a basic class declaration. Once again, we use the keyword
|
||||
| About the NetVisits, Inc Network | Write For Us | Advertise Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |
Visit other NetVisits, Inc. sites: |