JS base-Operator-function

Source: Internet
Author: User
Tags variable scope

1. Operators
1. Assignment operators and extension operators
1. Assignment operator =
2. Extension operators
+=,-=,*=,/=,%=,^= ....
Ex
A=a+b;--> a+=b;
A=a+1;--> A++,a+=1,++a
To implement two-digit transposition:
A^=b;
B^=a;
A^=b;
2. Conditional operators
The conditional operator is also called the trinocular operator.
The Monocular (unary) operator requires only one operand or expression.
ex:++,--,!
Binocular (two-yuan) operator, two actions or expressions required
ex:+,-, *,/,%,>,<,>=,<=,==,===,!=,!==,&,|,^,&&,| |
A trinocular (ternary) operator, which requires three expressions or operands.
Grammar:
Conditional expression? Expression 1: Expression 2;
The value of the conditional expression is evaluated first, and if the condition is true, the expression 1 is executed and the expression 2 is executed if the condition is false.
Ex
Enter the score from the pop-up box and if the score is greater than 60, the output will pass, otherwise the output will fail.
Practice:
Enter your score from the pop-up box:
If the score equals 100, the output is "award $1000",
If the result is >=90, the output "reward 100 Yuan",
If the score is >=70, the "Reward 10 element" is output,
Otherwise, the output "please everyone eat".
2. Functions
1. What is a function
Functions, function, also called methods
Function-function.
A function is a block of code that is predefined and can be executed repeatedly.
Pre-defined: Well defined in advance, not immediately executed.
Repeated execution: allowed to be called multiple times.
Code block: Can contain more than one executable statement
Used functions:
parseint ();
Parsefloat ();
Number ();
alert ();
document.write ();
2. Defining and Using Functions
1. Declaration and use of common functions
1. Disclaimer
Function name () {
function body-Several executable code
}
2. Calling functions
At any JavaScript legal location
function name (); A call to a function.
3. To invoke multiple functions at the same time, the following:
function Test () {
Calculate (); Calling function 1
print ();//Call Function 2
}

Practice:
1. Define a function name Calaulate (calculation)
In the function body, define two variables, NUM1, num2 and assign values to numbers, calculate the sum of two numbers, and print on the console.
2. Add a button to the Web page and invoke the function when the button is clicked.
2. Declaration and use of the parameter function
1. Disclaimer
Function name (argument list) {
function Body-JS Statement
}
Parameter list: You can declare 1 or more parameters, separated by commas between multiple parameters.
When declaring a function, the declared parameter is called a formal parameter.
2. Call
Function name (List of parameter values);
Note: Call as much as possible in the format of the declared function
When called, the value of the parameter passed is called an "argument".
Practice:
Define the parameter function, which calculates the product of two parameters and prints the output. The function is called when the button is clicked.
3. Declaration and use of function with return value
1. Disclaimer
Function name () {
function body
return value;
}

2. Call
Allows a variable to receive the return value of a function
var result= function name ();
Practice:
Defines a function that can receive three parameters, and the body of the function returns the number of three digits. and print the results to the console.
3. Scope of functions
1. Variable Scope
A scope represents an accessible range of variables or functions.
The scope in JS is divided into two types:
1. Function scope: Valid only within the function range
Function scopes, also known as local variables, are valid only in declared functions

2. Global scope: Any location of JS code is valid
Global scope is also called a global variable, and once declared, it can be used anywhere.
1. Variables not declared in function, as global variables
2. Declaring variables without Var, regardless of location declaration, is a global variable (not recommended)
2. Note
1. When global variables and local variables conflict, local variables are preferred.
2. Declaration in advance
Before the JS program is formally executed, function-declared functions pre-read all Var-declared variables to the top of the scope, but the assignment remains in the original position.
Functions also have the ability to declare in advance.
3. Passing by value
1. What is passed by value
Raw data types, which are passed as "value passing" when passing parameters.
Value passing: When a parameter is really passed, a copy of the value is actually passed (a value is copied) instead of the original value.
* Do not take the original data type to the function to modify the value.
Practice:
1. Create variable score, assign value to 90;
2. Create the function Changescore (num) again, and then output the num+5 in the function body.
3. Call the Changescore function and pass score the arguments to the function, and then output the score value.
3. Function scope
are divided into two types:
1. Local functions
Functions declared in a function
2. Global functions
The function defined at the outermost <script> is the global function, which, once defined, can be called from anywhere.
4.ECMAScript provides a set of global functions
1.parseInt ();
2.parseFloat ();
3.Number ();
4.isNaN ();
5.encodeURI ();
Url:uniform Resource Locator Unified Resource Locator, commonly known as: path
Uri:uniform Resource Identifier (identifier)
Function: Encodes the Uniform Resource identifier and returns the encoded string.
The so-called encoding is to encode the multibyte text in the address into single-byte text.
6.decodeURI ();
Function: Decodes the encoded URI and returns the decoded string.
7.encodeURIComponent ();
On the basis of encodeURI, special characters are allowed to be encoded.
8.decodeURIComponent ();
Allows decoding of special characters.
9.eval ();
Function: Executes a string representation of the JS code
Practice:
Define a function that receives a JS code (document.write ("5. Recursive invocation
Recursion: Call yourself again within a function.
Problem: Want to ask for factorial of 5
5! 5*4*3*2*1-->5*4!
4! 4*3*2*1-->4*3!
3! 3*2*1-->3*2!
2! 2*1-->2*1!
1! 1*1
Find the factorial of N and use a function to represent it?
function f (n) {
/*if (n==1) {
return 1;
}else{
Return N*f (n-1)
}*/
Return N==1?1:n*f (n-1)
}
Efficiency:
When this call is not finished, the next call is started, and the call is suspended until all calls are completed, and the more calls are made, the less efficient.
Practice:
The following series (Fibonacci series)
1,1,2,3,5,8,13,21,34,55 ...
Calculates the value of the nth number in a series by F (N).
Known:
The first and second numbers in the series are 1.
Starting with the third number, each number is the first two digits of the total.
F (1) =1
F (2) =1
F (3) =f (2) +f (1)
F (4) =f (3) +f (2)
...
F (n) =f (n-1) +f (n-2)

JS base-Operator-function

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.