JavaScript learning First, JS preliminary understanding

Source: Internet
Author: User
Tags array definition array length float double script tag

1. Introduction to JavaScript:
*javascript is an object-and event-driven language that is primarily used by clients.
--based on the object:
* * Provides a number of objects that can be used directly.
--Event-driven:
* * HTML-made Web pages are static effects, and JavaScript does dynamic effects.
--Client:
Mainly refers to the browser.

Features of *js:
(1) Interactivity:
--Dynamic interaction of information.
(2) Security:
--s cannot access files on the local disk.
(3) Cross-platform:
-unlike Java, JS uses a browser cross-platform directly.

The difference between *javascript and Java: (no association)

(1) Java is released by Sun Corporation (now Oracle)
JS is a company published by Netscape.
(2) JavaScript is Object-based and Java is object-oriented.
(3) Java is a strong type of language, JS is a weak type of language.
(4) JS is parsed can be executed, Java needs to be compiled before execution.

Composition of the *js
There are three parts.
(1) ECMAScript
--ECMA: The European Computer Association, which specifies the syntax for JS.
(2) BOM
--broswer the object model: the browser objects models.
(3) DOM
--document: Document Object model.



2, JS and HTML combination method (two kinds)
The first type:
--Use a label:
<script type= "Text/javascript" > JS code </script>
The second type:
--Introduction of external files:
--Create a. js file.
--<script type= "Text/javascript" src= "External file address" > </script>
       Note: Do not write other JS code in the script tag in the second type, and it will not be executed.

3. JS's original type and declaring variable
* * There are eight basic data types in Java: byte short int long float double char Boolean
In JS, there are only five primitive types:
--string: String
var str = "ABC";

--number: Number Type
var num = 123;

--boolean:true and False
var falg = true;

--null:
Gets a reference to the object, NULL to indicate that the reference is null, and that all objects have references to object
var date = new Date ();

--undifined
Defines a variable that is not assigned a value
* * * var AA;

     * * typeof (); View the data type of the current variable.
     * * Alert (); Jump out of a dialog box on the page.
     * * document.write ()The statement that directly wants the output of the page (you can output the statement in parentheses to the page)
You can also export HTML code.

4, JS of the statement
--The statement inside the Java:
If statement
Switch statement
Looping statements: For, while, Do-while

--There are also these statements in JS
If Judgment statement
      Note:=: Assign Value
= =: Judging

Switch statement
--java supports string, enum type, starting from 1.7,
All types are supported in--js.
--switch (a) {
Case 1:
Break
Case 2:
Break
Default
... ...
}
--Looping statements: for, while, Do-while: Consistent with usage in Java.

Among them, i++ and ++i are the same as in Java.



5. JS operator
* + =: x+=y; equals: X=x+y;

**js not distinguish between integers and decimals
var j = 123;
alert (j/1000*1000);
--java, the result is: 0
--JS, the result is: 123

Operation of adding and subtracting strings in **js
var str = "123";

In addition, the connection of strings is done.
When subtracting, the subtraction operation is done.

such as: alert (str+1)//result is 1231.
Alert (STR-1)//result is 122.

       Living in Italy: When STR is not a number, the subtraction operation will indicate an error.
var str = "ABC";
alert (str-1);//Result: Indicates Nan: This is not a number.

**boolean can also perform mathematical operations
If set to True, the equivalent value is: 1 when doing mathematical operations
If set to False, the equivalent value is: 0 when doing mathematical operations

* = = and = = = Difference
They're all used to make equal judgments,
= = Comparison is a value
= = = The value and type are compared
such as: var i = "5";
I==5 is: True
I===5 is: false.


6, 99 multiplication table exercises (output to page)
* Use: document.write () output to the page.
document.write can output variables or output HTML code (double quotes)
Inside the document.write is a double quote, and the label attribute must use double quotes.


7. Arrays of JavaScript
--unlike Java, JS is a weakly typed language that can hold different types of values in an array.
--java array definition: int[] arr = {£ º};

--js Array Definition method (three):
First: var arr = [three-way] | | var arr = [1, "2", true]
The second type: using the built-in object Array,
var arr = new Array (5); Defines an array of length 5.
Arr[0] = "1"; ARR[1] = 2; ARR[2] = true;

The third type: using built-in object Araay,
var arr = new Array (3, "4", true);//define an array where the value is: 3,4,true

--There is an attribute in the array length: Gets the size of the array.

--The length of the array is variable. (distinguished from Java arrays)

--arrays can hold different types of values (distinguished from Java arrays)


8, JS in the method

* * Define methods in Java:
Modifier return value type method name (parameter list) {
Method body;
return value;
}
public int Add (int a, int b) {
int sum = a+b;
return sum;
}

* * In JS, there are three ways to define the method:
The first way:
Use a keyword function
function method Name (argument list) {
Method body;
return value (depending on demand, optional);
}
For example:
function Add (A, b,c) {
var sum = a+b+c;
return sum;
}
(Call method name, use defined method)

The second way:
anonymous method.
var variable name = function (argument list) {
Method body;
return value;
}
Such as:
var add = Funtion (A, b) {
var sum = a+b;
return sum;
}
(The method is then called using the variable name)

The Third Way:
Dynamic methods.
Use a JS built-in object Function
var variable name = new Function ("parameter list", "Method body and return value"); (Note, a comma between the parameters and the method body)
var add = new Function ("A, B", "Var sum=a+b; return sum ");
(The function is then called using the variable name)
Note that in this way, the parameter list can be predefined, and the method body and method names are directly passed in when the method is defined.

9, JS global variables, and local variables
* * global variable: A variable defined in the script tag that can be used in the JS section of the page
-Used outside the method, used inside the method, and used in another scipt tag.

* * Local variable: A variable defined inside a method that can only be used inside a method.
--If used outside the method, an error is prompted.
--script5009: "NN" not defined
12-JS local variables. html, line 18 characters 3

**ie (Firefox) comes with a debug tool, IE8 and above, press F12, and the debug bar appears below the page.

10. Where the script tag can be placed

* * Script tags can be placed in any location (head,body, even HTML) in the format

* * However, in the development, in order to be able to smoothly and HTNL code combination, preferably placed behind the:</body>.

Cause: If you want to use values in the body (such as <input type= "text"/>) in the script statement,
and script Ben Shent in head, then there's going to be a problem.
HTML is parsed from top to bottom, and in this case the script uses the body
Value, but it has not been resolved to the body, you can not get this value.
So: Suggest script to be placed behind </body>.

11, the JS method of overloading

There are no overloads in **js, and when there are multiple methods with the same name but different parameters, the method is invoked using the parse order.
The method above it from its nearest definition. (if the parameter is not appropriate, an error will be: NaN)

JavaScript learning First, JS preliminary understanding

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.