JavaScript
  Home arrow JavaScript arrow JavaScript Objects: Strings
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript Objects: Strings
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-01-14

    Table of Contents:
  • JavaScript Objects: Strings
  • Length Property
  • Prototype
  • The Anchor() Method
  • The Big() Method
  • The Bold() Method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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 &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.

    More JavaScript Articles
    More By James Payne


       · Thanks for stopping by. In this article we talk about JavaScript String Objects,...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT