JavaScript
Alert (New Date ());
script>
JavaScript basic syntax
1. Script location
script>
2. Put it in a separate file
* .js
script>
3. Use the script program code as the attribute value
JavaScript is different from Java / C grammar:
type of data:
Integer, real shape (float, double?), Boolean, string
Variable declaration: (weak type language)
VAR name = "moon";
Undefined is not assigned, unknown
function:
Function pingfang (x) {
Return x * x;
}
Global Variables and Local Variables Problems / Passages & Command:
Var msg = "global variable";
Function show ()
{
MSG = "local variable";
}
SHOW ();
Alert (msg);
VAR i = 10;
Alert ("I Before:" i);
Function Change (i)
{
i = 10;
Alert ("i change" i)
Return I;
}
VAR J = Change (i);
Alert ("i after" i "/ tj" j);
script>
third page head>this is the third page font> p>
body>
html>
You can see it, it is a pass value, pay attention to and local variables, the difference between global variables.
If the variable name is the same, the local variable changes the value of the global variable (this is also the case in Java)! Note that the pass value is a completely different concept, the transfer function (method) value It is constant.
Package moonsoft.j2se.testbasic;
Public Class Testqujb
{
Public Testqujb ()
{
}
Public static void main (string args [])
{
INT x = 10;
System.out.println (x);
Change (x);
System.out.println (x);
INT i = 10;
System.out.println ("Quanju" i);
For (int J = 0; j <= 2; J )
{
i = 20;
INT K = 15;
System.out.print (i "/ t" k);
}
System.out.println ("After" i);
}
Public Static Void Change (INT X)
{
X = 10;
System.out.println (x);
}
}
JavaScript variable parameters:
Function testcanshu () {
Var params = ""
For (var i = 0; i Params = params "" arguments [i]; Alert (params); } Testcanshu ("ABC", "123"); Testcanshu (123, 456, 789, "abc"); script>
this is the third page font> p>
body>
html>
Arguments is an object, can not ignore the case, write Argument can't do it!
Create a dynamic function:
Var Square = New function ("x", "y", "var sum; sum = x * x y * y; return sum;");
Alert (Square (3, 2));
script>
JavaScript system function:
ENCODEURI
Decodeuri
Parseint
Parsefloat
Isnan
Escape
UNESCAPE
EVal
Object
JavaScript is an object-based language, not an object-oriented language, how do this sentence understand?
Create an instance of an object:
Var objinstance = new objName;
Function Person ()
{}
Var Person1 = New Person ();
Person1.age = 26;
Person1.name = "aaron";
Alert (Person1.AGe " Person1.name);
Function Say ()
{
Alert (Person1.name " Person1.Age);
}
Person1.say = SAY; // is more chaotic, one method can bring parentheses, you can bring a parameter
//, n parameters, compilers recognize !!!
Person1.say ();
script>
THIS keyword and overload research
Function Person (Name, AGE)
{
THIS.AGE = AGE;
THIS.NAME = Name;
}
Function Person (Name)
{
THIS.NAME = Name;
}
Function Say ()
{
Alert (this.name "," this.age);
}
Var person1 = New Person ("Aaron", 26); Var Person2 = New Person ("Kevin");
Person1.SAY = SAY;
Person2.SAY = SAY;
Person1.say ();
Person2.say ();
script>
Keyword this is only used in an object method, which is executed when a member method is executed, references the instance of the current object, with Java
About the overload of functions (methods), meaningless, in JavaScript.
Variable parameters are implemented with arguments, like top, it only chooses the least that function is implemented.
Is the value or a reference?
Function Change (i)
{i = 5;
Alert (i); // 10
}
VAR i = 5;
Alert (i); //5
Change (i);
Alert (i); //5
script>
The presentation value problem: a copy is created when the value is received, then exits the function, the copy disappears, the value is constant. So, JavaScipt is the value, with Java, in C / C / DEPHI In & Value can pass reference. Always pass the value!
By modifying the status of the object, it changes its value:
Function Add (Number) // Constructor
{this.Number = Number;
Alert (this.Number);
}
Function Say () // A method
{alert (this.number);
VAR J = New Add (5); // 5
J.SAY = SAY;
j.say (); //5
Function change () // change the status of the object
{
THIS.NUMBER = 5;
}
J.change = Change;
j.change (); // 10
j.say (); // 10
script>
Here, J IS A INSTANCE OF ADD FUCTION, SO, WE CAN SAY IT point points to the head pointer of a memory area, the content of this area is 5, and there is an ALERT (); then we have added a method: J . Say = SAY;
Then we added a method, j.change = change;
A copy is created here, but also points to this memory area, then we have modified this area, of course, will also change after exiting.
There are two objects in JavaScipt, one is needed, one is no need for NEW (static object)
Interior object of JavaScript
Object object
Function GetAttribute (Attr)
{
Alert (Person [attr]);
}
Var person = new object ();
Person.name = "aaron";
Person.age = 26;
GetaTtribute ("name"); // Note that this must be added to double quotes
GetAttribute ("age"); // Note that this must be added to double quotes
script>
It is the foundation of other objects, you can add attributes at will (, no inheritance concept ...)
String object
1) Attribute:
Var myfirststring = new string ("Moonsoft.blogchina.com"); // StringmyFriststring = "..."? No var lonngth = myfirststring.length; // 22
Alert (Length);
2) method
With the Java section, but Match, Link Methods, special
Method object
Static object
Date object
Var current_time = new date ();
Var strdate = current_time.getYear () "year";
STRDATE = Current_time.getMonth () 1 "Month";
STRDATE = current_time.getdate () "Day";
Alert (Current_Time);
Alert (strdate);
script>
STRDATE = Current_time.getMonth () 1 "Month";
Here and java.util. *; In January is 11 in December 11, so add 1.
Tostring
Transforming into a string of a format
TOSTRING (16); transform into 16-based
Specially used for the statement of the object
1.with (Object Instance) {}
If the statement of the above pop-up time is changed:
Var current_time = new date ();
With (current_time) {repeated loop to operate an object
Var strdate = getYear () "year";
STRDATE = getMonth () 1 "Month";
STRDATE = getDate () "Japan";
Alert (Current_Time);
Alert (strdate);
script>
2.FOR (Variable IN object) {} looped all attributes of an object
Array
Ordinary array:
Var Array1 = [1, 2, 3, 4, 5.56]; // NOTICE Here: Strange
For (VAR i = 0; i <= array1.length-1; i )
{
Alert (array1 [i]);
}
script>
Array of arrays
Var Array2 = [["A", "B", "C"], ["A", "B", "C"], 1, 2];
For (var i = 0; i <= array2.length-1; i )
{
Alert (array2 [i]);
}
script>
Suddenly, JavaScript does not care about the type of array, the strings and numbers can be put together, interesting.
Object array
Function ObjectArray (size)
{
THISLENGTH = Size; for (var i = 0; i { THIS [I] = ""; } } Var myObjects = New ObjectArray (3); MYObjects [0] = 3; MyObjects [1] = "Love"; MYObjects [2] = "3.14159265453"; VAR X, STR = "" For (x in myObjects) { STR = X ":" myObjects [x] "/ n"; } Alert (STR); script> There is a little difficult. Array object Convenient to sort, insert, delete mergers for arrays. Var arr1 = new array (); Var arr2 = new array (4); Var Arr3 = New Array (3.5, 4, "Arry", 789.5); Arr.sort (); The above is the basics of JavaScript. Next to attack DOM programming. Use JavaScript to interact with users. This is also our most willing.