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

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

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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