JavaScript Objects: Strings
(Page 1 of 6 )
In our last tutorial we covered JavaScript Errors. We learned to work with the OnError event, the Try...Catch statement, and Throw to create exceptions. In this episode we will go over JavaScript String Objects and learn to use them.
Objects, a Brief Introduction
Simply put, an object is a special for of data. In that sense, even a variable is technically an object. It consists of properties and methods. A property is the value you associate with an object. If your object contains a word like party, we could say that the property inside the object is a string that contains five characters. A method, meanwhile, is the action that can be performed on an object. For example, I could change all the letters of party to bold text, make them uppercase, and so forth.
Here is a table of available String Object Properties:
Property Name | What it Does |
constructor | References the function that created the object |
length | Results in the number of characters in a string |
prototype | Gives you the ability to add properties and methods to an object |
Constructor
As mentioned in the above table, a constructor can be used to tell you what sort of function created an object. Here is a sample of how the code works:
<html>
<body>
<script type="text/javascript">
var typeofcon=new String()
if (typeofcon.constructor==Array)
{document.write("The constructor is an Array")}
if (typeofcon.constructor==Boolean)
{document.write("This constructor is a Boolean")}
if (typeofcon.constructor==Date)
{document.write("This constructor is a Date")}
if (typeofcon.constructor==String)
{document.write("This constructor is a String")}
</script>
</body>
</html>
In the above code we create a variable named typeofcon and assign it the value of a new String function. We then use a series of If statements to determine what sort of function we used to create our variable typeofcon. Obviously we used a String function, and so the program will print out "This constructor is a String." Had we made it with a Date function, for example, it would have printed "This constructor is a Date."
In the next example we will make it a little more clear. Perhaps we want to know what variables we used to create an object, and how we set the values of those variables. Using the constructor property we can do just that:
<html>
<body>
<p>This is how <b><i>Studman ©</i></b> was constructed:<p>
<script type="text/javascript">
function superhero(firstname,lastname,superpower)
{
this.firstname=firstname
this.lastname=lastname
this.superpower=superpower
}
var studman=new superhero("Stud","Man","Looks Hot")
document.write(studman.constructor)
</script>
</body>
</html>
This will display:
This is how Studman © was constructed:
function superhero(firstname,lastname,superpower) { this.firstname=firstname this.lastname=lastname this.superpower=superpower }
So in layman terms, the constructor shows the function that created the object.
Next: Length Property >>
More JavaScript Articles
More By James Payne