JavaScript-basic syntax (2)

Source: Internet
Author: User

JavaScript-basic syntax (2)
If statement

Var a = 3; if (a = 4) {alert ("yes") ;}else {alert ("no") ;}// no, but modify it, if (a = 4), yes is printed, and a is assigned to 4 ,! 0 // so the constants are usually left, if (4 = x). if you delete =, an error is returned.

The rest is the same as java
Switch statement
The usage is the same as that in java, but the received type is many.

Use of switch and if:
If it is an interval or the structure is boolean, if
If you determine a few fixed values, you can use the switch. When it is loaded, the statement will enter the memory, and the answer can be selected in the memory.


Loop statement:

Var x = 1; document. write (""); while (x <5) {// alert ("x =" + x); // document. write ("x =" + x +"
") // Write data to the current page document. write (" x = "+ x +"
"); X ++;} document. write (" "); for (var I = 0; I <5; I ++) {// stop writing. Write it as int I document. write ("I =" + I +"
");} W: for (var I = 1; I <3; I ++) {// w is the label, for (var j = 1; j <3; j ++) {document. write ("I =" + I); break w; // The cycle where the exclusive label is located }}

Array:

Js arrays define two methods:
1. var arr = [1, 2, 3], var brr = [];
2. var arr = new Array ();

// The first document. write ("============================================ =
"); Var arr = [1, 2, 3]; // alert (typeof (arr); // object type, object // alert (" len = "+ arr. length); // array length. arr [1] = 250; arr [4] = 666; // Js array features, the array length is variable arr [2] = "hello"; arr [5] = true; // Js array features, any element type for (var I = 0; I ");} // PS: it is best to store the same type of document for Array Operations. write ("============================================ =
"); // Second // var brr = new Array (); // equivalent to var arr = []; // var brr = new Array (10); Array definition, and the length is 10 var brr = new Array (10, 9, 8, 7, 6); // Array definition, the element is 10, 9, 8, 7, 6 document. write (brr [2] +"
");

Function:

Definition Format: defined by the specified keyword
Function Name (parameter list)
Function body
Return value; (if no specific return value exists, return can be omitted)
Function sum (x, y) {// because the content is unknown, you do not need to define var return x + y;} alert (sum (3, 1); // out of use

Details:
1. If the function name is used, the function is called.
2. The function has an array (argument), which is stored in the input parameters.
Function sum (x, y) {alert (x + ":" + y) ;}sum (); sum (3); // 3: undefined. It can be seen that Js does not have an overload, when a function defines several parameters, it should pass several functions add (x, y) {alert (arguments. length); // 5 for (var I = 0; I ") ;}} add (1, 2, 4, 5 );


3.

Function sum (x, y) {return 100;} var he = sum (); // If var he = sum ;, the code of the sum function will pop up: alert ("he =" + he); var hee = sum; alert (hee); // you want to use alert (sum) alert (hee (100); // function show () {alert ("run");} alert (show (); // print run, undefined

Js is based on objects, so the underlying layer is all objects. The function name is actually the underlying Object Name, var he = sum (); normally, 100 is returned. If no brackets are written, the sum address referenced by the object is directly assigned to he, that is, the function object has two function names, and then executes alert. If he points to an object, it is printed as a string, is the code Definition Format of the function.

Dynamic functions:
* Js dynamic Functions
* An object Function built in JS is used.
* Not many functions are used.
* Parameter list. The function bodies are dynamically specified by strings.
* The parameter list and function body of common functions have been written to death and cannot be changed.
*/

function add(x1,y1){                return x1+y1;            }            var add1 = add(1,2);            alert(add1);            var result = new Function("x,y","var sum; sum = x+ y;return sum;");            var sum = result(1,2);            alert("sum = "+sum);

Anonymous functions:

// Anonymous function: no name, abbreviated form of Function
Var result = function (a, B) {return a + B;} alert (result (1, 2 )); /* ============================================================= */function H () {alert ("H");} var HH = H ();/* the abbreviated form is as follows */var hh = function () {alert ("H ");}

Quantum variables and local variables:


The global variables of Js are significantly different from those of Java.
<script type="text/javascript">        for(var x = 1;x<5;x++){            document.writeln("x = "+x);        }        document.writeln("x = "+x);    </script>    <script type="text/javascript">        document.writeln("x = "+x);    </script>

// The scope of x defined in for is not only valid in its local <script> segment, but can be simply understood as effective throughout the page


Local variables of Js:
<Script type = "text/javascript"> function show () {var x = 1; // The variable defined in the function is local} document. writeln ("x =" + x); // No access </script>

PS: the variables defined in the script are global and the variables defined in the function are local. It is best to have different definitions to avoid unnecessary problems.

Var x = 3; // global x function show (x) {x = 8; // local variable x} show (x); document. writeln ("x =" + x); // 3

Common Js objects:

1. Object: god of all objects
Method:
(1). toString (): Convert the object into a string
Function show () {alert ("run");} alert (show. toString (); var arr = [1, 2, 4]; alert (arr. toString (); // toString, can be omitted var AB = function () {alert ("ni");} alert (AB );

(2). valueOf (): similar to toString (). Usage object. valueOf ();
 var arr = [1,2,3,4];        alert(arr.valueOf());var obj = new Object("asd");//-> var obj = new Object() , obj = "asd";alert(obj.valueOf());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.