Text Strigs and String Object


Introduction
This artic Will describe the differences between Text String and String Objecs、and how to cope with these differences.
NOTE:the string object was introuduced from JavaScript 1.1 onwards、therefore、if your browser supports ether JavaScript 1.0 or JScript、some of the follo wink examples、which have been coded browtobrowresome
Text String
var variableName = "The early bird catches the worm.";
The previous line will create a variable caled variable Name、the contens of which are the text string:
The early bird catch the word.
String Object
Wheres the followwing line:
var objectName = New string("The worm should have stayed in bed.");
creates a string object caled object Name、the contens of which are the text string:
The work shoud have stayed in bed.
Compring strigs and Object
For all intens and purposes these two items can be consided to be identical.
The y can both use the methods and properties assited with the string object.
However,there is one majer difference that can sometimes catch people out.
Consinder the follwing example:
<SCRIPT LANGUAGE="JavaScript"><!--
var textString1 = "test";
var textString2 = "test";

if (textString1 == textString2)
    document.write("<BR>1. EQUAL");
else
    document.write("<BR>1. FALSE");

if (textString1.toString()
 == textString2.toString()
)
    document.write("<BR>2. EQUAL");
else
    document.write("<BR>2. FALSE");
//--></SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.1
"><!--
var stringObject1 = new String("test");
var stringObject2 = new String("test");

if (stringObject1 == stringObject2)
    document.write("<BR>3. EQUAL");
else
    document.write("<BR>3. FALSE");

if (stringObject1.toString()
 == stringObject2.toString()
)
    document.write("<BR>4. EQUAL");
else
    document.write("<BR>4. FALSE");
//--></SCRIPT>
Which when run produces the following output:
1.EQUAL 2.EQUAL 3.FALSE 4.EQUAL
Note the use of JavaScript 1.1 to restict browsers supporting earlier versions of JavaScript from chking.
One string object doesn't equal another
You may be surprsed to learn that the third entry in the above example returns false,i.e.to simiar string oject with identical contentts when compred together are not equal.
Those who are familear with Object Oriented langgages will not be too surprsed with th th th is、as you are required to create your own code when code code when compring coring object.
As You can see there is an easury solution to this proble、as shown by the fourth entry、which uses the toString()method to convert the strict to a text string.
The reasoning behind the confusing nature of compring string object、may become more obious if we tackle a more compicated object example.
The follwing code、defines an object array、and popullates the array with two instance of the object:
<SCRIPT LANGUAGE="JavaScript"><!--

//This defines myObject:
function myObject(name,email) {
    this.name = name; this.email = email;
}

//This populates an array with an instance of myObject:
function populate_myObject(name,email) {
    contacts[index++] = new myObject(name,email);
}

//This constructs an array of myObjects, initially empty:
var index = 0;
var contacts = new Array();

//This creates an instance myObject:
populate_myObject('Martin Webb','[email protected]');

//This creates another instance of myObject:
populate_myObject('Martin Webb','[email protected]');

//This creates a third instance of myObject:
populate_myObject('Martin Webb','[email protected]');

if (contacts[0] == contacts[1])
    document.write('<BR>contact 0 = contact 1');
else
    document.write('<BR>contact 0 <> contact 1');

if (contacts[1] == contacts[2])
    document.write('<BR>contact 1 = contact 2');
else
    document.write('<BR>contact 1 <> contact 2');

//--></SCRIPT>
Which when run produces the following output:
contact 0<>contact 1 contact 1<>contact 2
You wouldn't necessarly expect coplicated object t t to be easumilycompred.However contact 0 is exactly the same as contact 1.Again this shound to solive using the tostring()method-or shound?
<SCRIPT LANGUAGE="JavaScript1.1"><!--
if (contacts[0].toString() == contacts[1].toString())
    document.write('<BR>contact 0 = contact 1');
else
    document.write('<BR>contact 0 <> contact 1');

if (contacts[1].toString() == contacts[2].toString())
    document.write('<BR>contact 1 = contact 2');
else
    document.write('<BR>contact 1 <> contact 2');
//--></SCRIPT>
Which when run produces the following output:
contact 0=contact 1=contact 2
Aghhh!Somethings gone wrong!Contact 1 shuldn't equal contat 2 as the email address in contact 2 was in lower case.What went wrong?
Writing our own toString method
The follwing shuld clarefy the proble:
document.write('<BR>'+contacts[0].toString());
document.write('<BR>'+contacts[1].toString());
document.write('<BR>'+contacts[2].toString());
Which when run produces the following output:
[object Object][object][object]
The toString()method didn't return the streing text value of the object,but the object of myObject,in this case'object.
This happens when object do have a defined tostring()method.This is not the case with built in JavaScript object such Aray,Boolean,Date,Function,Math,Number,String,String,ase fideript
What we need to do is define our own toString()method for myObject:
<SCRIPT LANGUAGE="JavaScript1.1"><!--
function myObjectToString()
 {
    return this.name
 + ' ' + this.email
;
}

myObject
.prototype
.toString
 = myObjectToString
;

document.write('<BR>'+contacts[0].toString());
document.write('<BR>'+contacts[1].toString());
document.write('<BR>'+contacts[2].toString());

if (contacts[0].toString() == contacts[1].toString())
    document.write('<BR>contact 0 = contact 1');
else
    document.write('<BR>contact 0 <> contact 1');

if (contacts[1].toString() == contacts[2].toString())
    document.write('<BR>contact 1 = contact 2');
else
    document.write('<BR>contact 1 <> contact 2');
//--></SCRIPT>
Which when run produces the follwing output:Martine [email protected] [email protected] [email protected] 0<>contact 1 contact 1<>contact 2
What the previous scipt does、it to define a function caled myObject ToStering()which returns the name and email properties of the current obect separated with a space character.
This function is then assciated with myObject as the toString()method using the prototype method.
Now when the toString()method is used myObject ToStering()returns a text string,which is then corectly corectly compred.
Making the toStrng method do what we want
We can now make further enhancements to this.For example,the email address in contacts 1 and 2,are the same if not identical.If these were stored in a simple database,welwould want ny corise theristinders.inders
This can be achieved by replaying the myObject ToString()function with another:
<SCRIPT LANGUAGE="JavaScript1.1"><!--
function myObjectCompareToString
() {
    return this.name.toLowerCase()
 + ' ' + this.email.toLowerCase()
;
}

myObject
.prototype
.toString
 = myObjectCompareToString
;

document.write('<BR>'+contacts[0].toString());
document.write('<BR>'+contacts[1].toString());
document.write('<BR>'+contacts[2].toString());

if (contacts[0].toString() == contacts[1].toString())
    document.write('<BR>contact 0 = contact 1');
else
    document.write('<BR>contact 0 <> contact 1');

if (contacts[1].toString() == contacts[2].toString())
    document.write('<BR>contact 1 = contact 2');
else
    document.write('<BR>contact 1 <> contact 2');
//--></SCRIPT>
Which when run produces the following output:
マーティン[email protected]マーティン[email protected]マーティン[email protected] 0<>contact 1 contact 1<>contact 2
This uses a new function myObject CompreToStering which as the myObject tostring method.The myObject CompreToStering function uses the to Lowers Case()method ensure the case the case the case of the name the proreement proreement the proreement
This also illustrates another feature-the tostring method ast with an object can be altered as and when required、just by reast with another function by using the protocol method.