Objects and Properties
Join the Discussion
What this means is that the variables that you define are in fact also objects (to use the Object oriented term) and as objects they have access to the methods associated with their class. These objects also have properties, the most obvious of which is the value that you assigned to it but there can be others.
In this tutorial we are going to look at the methods and properties associated with the classes of objects (variable types) that we have already learned about. Later we will learn about some other built in classes you can use to define objects.
Properties are the values associated with an object. A boolean object has only one property, the value of the object - which is either true or false. Numeric and text (or more properly - string) objects have other properties in addition to their value.
Properties (other than the actual value of the object) are accessed by placing a dot between the name of the object and the property that we want to refer to. For example we can determine the actual length of the content of a text field by referencing the length property like this:
In addition to properties, there are also actions that can be performed on objects that are specific to the type of object that we have (its class). These actions are called methods and they are very similar to functions except that they are specific to objects belonging to the particular class.
Methods are accessed by placing a dot between the name of the object and the method that we want to refer to and since methods are like functions we can pass parameters to methods just like we can with functions.
Both boolean and number fields can be converted to text using the toString() method like this:
There are other methods available that you can use with number objects but the class of objects that we have met so far with the most methods are strings. The original string methods were created before you could place HTML tags in your document.write statements and produced the equivalent formatting, these don't get used very much any more. The main methods that you will use with your text fields are those that will manipulate the text itself. Some examples of these include substr(start,length) which extracts part of the string, split(chr) which breaks a string into pieces at a given character, and toLowerCase() which converts the entire string to lowercase.
Using What You Know
By now you will have noticed that document.write() is itself using a method called write() to perform an action on an object called document. The document object refers to the current page displayed in the browser and the write method writes to it. We'll look more at objects like document in a future tutorial.
What we can do at this point is look at how you might go about extracting the character at a specific position in a text string. We'll set up code to take a number and extract the corresponding letter of the alphabet. Here's the code:
var str = 'abcdefghijklmnopqustuvwxyz';
The characters in the text string are numbered from 0 so we need to subtract one from the number for the position that we want in order to get the right position in the string. The above code will of course write out "e" which is the fifthe letter in the text string.