The basics of JavaScript

Source: Internet
Author: User

I. Differences between JavaScript and java
1. JavaScript is a product of Netscape and Java is a product of Sun.
2. JavaScript is based on objects and Java is object-oriented.
3. JavaScript can be executed only after interpretation. Java needs to be compiled into a bytecode file before execution.
4. JavaScript is weak type, and Java is strong type.
Summary: In fact, apart from the name of java and JavaScript, JavaScript has borrowed some java ideas, and the rest is almost irrelevant.
2. How to combine JavaScript with Html
1. Tag format
We store JavaScript code in the tag pair <script>... </script>. Can be placed anywhere.
2. Import Method
Use the src attribute of the script tag to introduce a JavaScript file.
Example: <script src = "test. js" type = "javascript"> </script>
Note: The type attribute must be added to the script label in the specification.
Iii. JavaScript syntax
1. Variables
It is defined by the keyword var. Because it is a weak type, you do not need to specify a specific data type.
For example, var x = 3; x = "hello ";
Note: 1) There is no difference between single quotation marks and double quotation marks in JavaScript, but it is best to write double quotation marks.
2) Special constant value in JavaScript: undefined. It is used when the variable is not initialized. The variable value is undefined (undefined ).
2. Operators
Like other programming languages, it is similar to java. It also supports string connectors (+) and ternary operators (? :), The difference is that the ternary operator does not need to have a value, and can be directly output in the statement.
3. Statements
The statement format is similar to that of various programming languages. You can also determine, select, and loop the statements. But pay attention to the following points:
1) In JavaScript, non-0 is true. For example, the following code
Var x = 3;
If (x = 4) // you can perform comparative operations.
If (x = 4) // you can perform the value assignment operation and perform the same judgment. No error is reported. The result is true. Therefore, if (4 = x) can be written after the if statement. if it is 4 = x, an error is returned. Errors can be corrected.
2) the switch has no type restrictions.
3) The cycle must have an ending condition.
4) to connect a boolean operation, you must use & or |. If & or | is used, bitwise operations are performed.
Iv. Functions
1. General Functions
Format:
Function Name (formal parameter ...)
{
Execute the statement;
Return value;
}
Note: 1) The return statement can be left blank and the function must be called before it can run.
2) There is no need to add var for a formal parameter, which is a weak type.
3) call a function with parameters, but do not pass the value to it, or pass a value greater than the number of parameters. A function can also run or call a function without parameters, pass the value to it, and the function runs the same way.
4) there is no form of overload function in JavaScript.
Because multiple parameters of the function are actually encapsulated in an arguments array in JavaScript, you can accept any number of parameters, but it is best to pass the actual parameters in the defined form.
5) Pay attention to the following example:
Var show = demo ();
The preceding statement indicates that the show variable receives the demo function's return value.
Var show = demo;
The preceding statement indicates that show and demo represent the same function and point to the consent object.
2. Dynamic Functions
Implemented through the built-in object Function of JavaScript. As follows:
Var demo = new Function ("x, y", "var sum = x + y; alert (sum );");
Demo (5, 2 );
Dynamic functions are different from general functions. Parameters and function bodies can be passed through parameters and can be dynamically specified.

3. Anonymous Functions
Format: function ()
Example: var demo = function () {alert ("snow ");}
Demo ();
Note: It is usually used to define the behavior of event attributes.
4. Other Forms
Var p = new Object ();
P. name = "lisi ";
P. age = 31;
P. demo = show;
Alert (p. name + ":" + p. age + demo (4, 5 ));
Function show (x, y) {return x + y ;}
5. Array
In JavaScript, arrays combine the features of collections to store any element, and the length is variable. In addition, when assigning values, it is not braces but brackets, as shown below:
Var arr = new Array ();
Arr [0] = "ashf ";
Arr [1] = 258;
Or var arr = ["ashf", 258, true, 'sfa'];
Note that int x is not required during traversal, var x is required, and weak type is required.
Two-dimensional array: var arrr = [[element, element...], [element...], [element...]
6. built-in objects
Many built-in objects, such as String, Object, Date, and so on, can be found in the relevant help documentation (if necessary, you can leave a mailbox) in JavaScript ), here we will briefly describe some of the differences.
1. length in String is an attribute, not a method
2. In String, the substring (a, B) method is a character from a to the B-1, while the substr (a, B) method starts from a and takes B characters
3. a. toString (B) returns the form of B in hexadecimal notation.
4. parseInt ("a", B) converts a in B to a in decimal form.

VII. Custom object
In JavaScript, besides provided built-in objects, you can also customize objects.
Method 1:
Function car (){}
Var c = new car ();
C. color = "white ";
C. tyre = 4;
C. run = show;
Function show () {alert (c. color + c. tyre + "run ");}
C. run ();
Method 2:
Function car (color, tyre)
{
This. color = color;
This. tyre = tyre;
}
Var c = new car ("white", 4 );
Alert (c. color + ":" + c ["tyre"]);
8. Statements used to operate objects
1. with statement
When multiple members of an object are called, to simplify the call and avoid repeated writing in the format of "object.
Format: with (object) {the code here does not need to be added (object. Attribute}
Var p = new car ("white", 4 );
Alert (c. color + ":" + c ["tyre"]);
Can be written:
Var p = new car ("white", 4 );
With (car) {alert (color + ":" + tyre )};
Note: The with statement defines the scope of an object. In this domain, you can directly call the members of this object.
For... in statement
For variable in object statement: traverses the members of an object. If you traverse an array, the variable contains the script
Obtains the value of an object. The variable is the property name.

Related Article

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.