Home arrow JavaScript arrow JavaScript Objects: Strings
JAVASCRIPT

JavaScript Objects: Strings


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.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
January 14, 2008
TABLE OF CONTENTS:
  1. · JavaScript Objects: Strings
  2. · Length Property
  3. · Prototype
  4. · The Anchor() Method
  5. · The Big() Method
  6. · The Bold() Method

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JavaScript Objects: Strings
(Page 1 of 6 )

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 &copy;</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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials