JS learning travelogue 2. js learning travelogue

Source: Internet
Author: User

JS learning travelogue 2. js learning travelogue

1. The scope of a variable is declared outside of all functions. It is called a global variable because it can be accessed by any other code in the current document. The variable declared in the function is called a local variable because it can only be accessed within the function. For example, define variable x outside the function and then use it in the function. Define the variable y in the function and use it outside the function.

x=12;function  myfunc() {    alert(x);    var y=110;}myfunc();document.write(y);

2. escape characters

All ASCII codes can be represented by "\" and numbers (generally Octal numbers. C defines some common ASCII characters that cannot be displayed, such as \ 0, \ t, \ n, which are called escape characters, because the subsequent characters do not mean their original ASCII characters. What does it mean? That is to say, after \ is added, it indicates that the meaning of the original character is no longer changed. The students can understand him in this way.

Escape characters

ASCII value (decimal

Meaning

\

007

Bell (BEL)

\ B

008

Return to BS to move the current position to the previous column.

\ F

012

Change page (FF), move the current position to the beginning of the next page

\ N

010

Line feed (LF), move the current position to the beginning of the next line

\ R

013

Press enter (CR) to move the current position to the beginning of the line

\ T

009

Horizontal tabulation (HT) (jump to the next TAB)

\ V

011

Vertical tabulation (VT)

\\

092

Represents a backslash character ''\'

\'

039

Represents a single quotation mark (apostrophes) character

\"

034

Represents a double quotation mark character

\?

063

Represents a question mark

\ 0

000

NULL)

\ Ooo

Three octal nodes

Any character from 1 to 3 octal digits

\ Xhh

Two hexadecimal

Any character in hexadecimal notation between 1 and 2

3. Custom object

Function person (username, age, job) {this. username = username; this. age = age; this. job = job;} var person = {username: "Xi Da", age: 50, job: "President"} document. write (person. username );

4. typeof

Typeof is used to return the data type. We will first tell the students about the returned values of this function and the data types they represent, so that they will not be confused when they see the examples below.

Data Type corresponding to the returned value

Undefined: The value is undefined.

The value of boolean is a boolean value.

The string value is a string.

The value of number is a numerical value.

The object value is an object.

The value of function is a function.

5. function return value

Function mfuc () {var name = "Xiaohu"; return name;} document. write (mfuc ())

6. Internal functions

Internal functions, also known as js built-in functions, are built-in functions in the browser kernel and can be directly used without introducing any function libraries. The following describes all internal functions, but you don't have to worry about so many functions. There is no need to memorize it here. You only need to browse it a few times and know about its functions. Then, you need to study its usage and functions in detail.

Avascript built-in functions can be divided into five types:
1). Regular Functions
(1) alert function: displays a warning dialog box, including an OK button. (2) confirm function: displays a confirmation dialog box, including the OK and Cancel buttons. (3) escape function: converts a character to a Unicode code.
(4) eval function: calculates the result of the expression.
(5) isNaN function: test whether (true) No (false) is not a number.

(6) parseFloat function: converts a string to a number of vertices.

(7) parseInt function: converts a string to an integer number (number of digits can be specified ).

(8) prompt function: displays an input dialog box, prompting you to wait for user input.

2) array Functions

(1) join function: converts and connects all elements in the array to a string.

(2) langth function: returns the length of the array.

(3) reverse function: reverses the order of array elements.

(4) sort function: sorts array elements again.


7. Closure

f1(){    n=999; nAdd=function(){n+=1} function f2(){        alert(n); }    return f2; }

Here, the function f1 has a local variable which is invisible outside function f1. However, through the f2 function, we can bring up the value of the local variable, which is also the biggest use of the closure.

8. functions as parameters

Function test1 (Func) {Func ();} function test2 () {alert ("I'm test2");} test1 (test2 );

Note:
Here, test1 has a parameter, which is a function. Define another function test2, and use it as the parameter of test1.
Result:


9 string replacement

Var str = document. getElementById ("demo "). innerHTML; var n = str. replace ("test", "insert data information"); document. getElementById ("demo "). innerHTML = n;

10. concat ()

var txt1 = "Hello ";var txt2 = "world!";var m=txt1.concat(txt2);document.getElementById("demo").innerHTML = m;

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://blog.csdn.net/kangguang/article/details/79353719

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.