
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
90
|
Chapter 4: Primitive Datatypes
You may encounter the deprecated Flash 4 string concatenation operators & and add
when examining legacy code. The
+ operator should be used in Flash 5 and later. See
Appendix C for more information on backward compatibility.
The concat() function
The concat() function appends characters to a string, similar to +=. Because concat()
is a function, it uses the dot operator, like this:
var product = "Macromedia".concat(" Flash");
var sentence = "How are you";
var question = sentence.concat("?")
Take heed, though—unlike +=, the concat() function does not alter the string that it
is applied to; it merely returns the concatenated string value. In order to make use of
that value, we must assign it to a variable or other data container. Study the follow-
ing code closely, so you’ll understand the difference between
+= and concat():
// Here we mistakenly forget to store the return of concat( ) in a variable.
var userName = "Karsten";
var greeting = "Welcome to GreatSite.com";
greeting.concat(userName);
trace(greeting); // Displays "Welcome to GreatSite.com"
// The value of greeting was unaffected by concat( )
// Here we properly store the return of concat( ) in a variable.
finalGreeting = greeting.concat(" " + userName);
The concat( ) function also accepts multiple