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  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Developerworks
 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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

    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...
    - Improving an Image Zooming Application with ...
    - Zooming in on Images with JavaScript
    - JavaScript Date Objects: Universal Coordinat...
    - Javascript Objects: More Date Methods
    - JavaScript Objects: Dates
    - JavaScript Objects: Finishing Strings

    Iron Speed

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway