This article complements some of the details of the previous JavaScript simple entry .
First, global variables and local variables
The variables defined within the <script> tag are global variables in the current page. That is, the <script> tag can directly access variables defined by other <script> tags. The variables defined in the function body are local variables. Such as:
1 <script type= "Text/javascript" >2var x = 1; // Global Variables 3 function Show (x) {// local variable 4 x = + +x; 5 return x; 6 }7Show (x); 8 document.write ("x=" +x); 9 </script>
So the output x = 1; not 2.
second, common objects
1 . Object: Provides a common method for JS objects. such as the ToString () method: Returns the string form of the object.
2 . String object: Used to process text (string), a string object provides many methods for a string.
① strings are a basic type of data for JavaScript. The length property of the string object declares the number of characters in the string. The string class defines a number of methods for manipulating strings, such as extracting characters or substrings from a string, or retrieving characters or substrings. It is important to note that the JavaScript string is immutable (immutable), and the method defined by the string class cannot alter the contents of the string. A method like String.touppercase () returns a completely new string instead of modifying the original string.
② users can use existing methods to add custom methods, such as: remove spaces at both ends of a string:
1<script type= "Text/javascript" >2 functionTrim (str) {3 varStart,end;4Start = 0;5End = Str.length-1;6 7 while(Start <= end && Str.charat (start) = = "){8start++;9 }Ten while(Start <= end && Str.charat (end) = = "){ Oneend--; A } - returnStr.substring (start,end+1);//contains headers that do not contain tails. - } the</script>
③string's prototype: String.prototype
You can use prototype properties to add properties and methods to an object. New methods or properties are added, then the string object has these new methods. Such as:
<script type= "Text/javascript" >//To add a custom method using the string prototype haha ():String.prototype.haha=function(){ varStart,end; Start= 0; End= Str.length-1; while(Start <= end && Str.charat (start) = = ") {Start++; } while(Start <= end && Str.charat (end) = = ") {End--; } returnStr.substring (start,end+1);//contains headers that do not contain tails. }//The string object can then call the Haha () method.
3. Array object: Arrays Object
methods such as: Concat (): Returns a new array, which is a combination of two or more arrays.
4. Date Object : Date Object
Enable basic storage and obtain the date and time.
5. Math Object : is an intrinsic object that provides basic mathematical functions and constants. You can call directly without creating an object with new.
third, special statement: with
The WITH statement is often used to shorten the amount of code that must be written in a particular situation. In the following example, note the repeated use of Math :
1 x = Math.Cos (3 * math.pi) +2 y = Math.tan (* math.e)
When using the WITH statement, the code becomes shorter and easier to read:
1 with (Math) {2 x = cos (3 * PI) + sin (LN10) 3 y = tan (* E)
4 }
JavaScript Simple Primer (Supplemental article)