Knowledge points required for Javascript Review

Source: Internet
Author: User

1. interpreted and compiled languages
Compilation type:
First, let's talk about the code we have written, convert it into bytecode that computers can understand, and then save these bytecode to generate an executable file, execute this executable file (JAVA, c) as needed next time)

Explanatory type:
First, let's talk about the code we wrote and convert it into bytecode that can be understood by the computer. Then, instead of saving it to generate executable files, we directly execute the code (php, javascript)


2. javascript variable scope
Global scope: All javascript executable scopes take effect.

Local scope: the current function takes effect.
For example:
If the function defines the same variables as the function, the function operates on local variables and the global variables of the function's external operations.

Varv = 'global ';
Functionf1 (){
Varv = 'function ';
Alert (v );
}
F1 ();

② Var v = 'global ';
Functionf1 (){
Alert (v );
Varv = 'function ';
}
F1 ();

Why is it undefined?
Javascript code execution process:
Javascript code is executed according to the html document stream, but not completely followed. Instead, the function is preferentially executed. At this time, the local variable inside the function is temporarily set to undefined.

3 ③
Varv = 1;
Functionf1 (){
V + = 1;
Alert (v );
}
F1 ();
F1 ();

Javascript data type: (8 big data types)
Basic data type (raw data type)
Undefined
Declared but not initialized (first assignment)
The function has no return value.
Null
Unfound (undefined object), an object
Boolean
True, false
Number
Integer and decimal are both numerical types.
String
Javascript is an object-based language because it has no class concept.
All data is objects,
People-object: Russian, American, Chinese
Varstr = 'Hello world ';
Varstr2 = 'nihao ';
// Alert (str. length );
// Alert (str. concat (str2 ));
Alert (str. toUpperCase (); // when the verification code is case insensitive
Alert (str. substr (2, 3); // parameter 1, index of the character to be truncated, parameter 2, length of the truncated

Object Data Type (reference data type)
1, array
// Obtain the Array
Vararr = ['xiaobai ', 123];
Vararr2 = new Array ('xiaohei', 456 );
// Reference data type (the same address is referenced)
Vararr3 = arr2;
Arr2.push ('hello ');
// Alert (arr3 );
Varstr = arr2.toString ();
Alert (typeof (str ));
For... in
// Traverse the elements of the array
Vararr = ['zhangsan', 'lisi', 'wangwu', 'zaholiu'];
Vari;
For (iin arr ){
Alert (arr );
}

2. Object
Get object
(1) new Object ()
(2) Get the object through the constructor (constructor)
// Obtain the object through the constructor
Function student (){
This. name = "wanger ";
}
Var obj = new student ();
Alert (obj. name );
(Constructor is a common function. It obtains an object when we call it through the new function name (). At this time, this function is called a constructor, this is usually used inside the constructor to represent the current object.
(3) json {}
Varobj = new student ();
Alert (obj. name );
// Get it using the json shortcut syntax
Varobj2 = {'name': 'hangsan '};
Alert (obj2.name );

Javascript built-in object
Job: Compare floor ceil round differences
Varnum= 0.123;
Alert (Math. ceil (num); // 1
Alert (Math. round (num); // 1
Alert (Math. floor (num); // 0

Random Name:


Date object digital clock



Function object
// The function is a value, because we can use a variable to save it. We can find this function through the variable (function name ).
Varf1 = function (){
Alert ('hello ');
}
F1 ();
Alert (typeof (f1 ));

Alert ()
Window. alert ();
Function scope chain:
The process of searching from the lowest level to the next level

Function parameters:
The number of parameters of a function can be different when called and declared. Because there is an object inside the function that specifically manages function parameters, this object can obtain real parameters.
Evaluate the sum of function parameters
Function f2 (n1, n2, n3, n4 ){
Alert (arguments [1]);
}
F2 (1, 2, 4, 5 );

Confirm
<P id = "p1"> delete me </p>
<Scripttype = "text/javascript">
// Alert (confirm ('Are you sure you want to delete it '));
// Obtain the button clicked by the user (confirm or cancel). If yes, the return value is true. If yes, the return value is false.
If (confirm ('Are you sure you want to delete it ')){
Document. getElementById ("p1"). innerHTML = '';
}
</Script>

Guess number
Alert (prompt ('Enter the number', 2 ));
// Generate a random number
// Compare the numbers entered by the user with those randomly generated
// Specified number of times
Varrandom = Math. random () * 100;
Varinput = prompt ('Enter the number', 2 );
If (input <random ){
Alert ("the number you guessed is too small ");
} Elseif (input> random ){
Alert ("the number you guessed is too big ");
} Elseif (input = random ){
Alert ("Congratulations, you guessed it ");
}


The Eval () function runs the function parameters as js expressions.
For example, eval ('1 + 2') returns 3
In the input form, if it is a text box of the text password type, the size attribute specifies the number of characters that can be entered by the user.
For other attribute forms, size indicates the size of the occupied pixels.

Note:
Use innerHTML to obtain the TAG content
Obtain the value of the form using value

Calculator
Core code
/*
Obtains the number entered by the user to bind the onclick event
Start Operation
Result
*/
Varflag = true;
FunctiongetNum (num ){
If (! Flag ){
Document. getElementById ('res'). value = '';
Flag = true;
}
Document. getElementById ('res'). value + = num;
}
FunctiongetRes (){
Varnum = document. getElementById ('res'). value;
Num = eval (num );
Document. getElementById ('res'). value = num;
Flag = false;
}

 

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.