JS Getting started with heart knot

Source: Internet
Author: User


Learning the basics of HTML and CSS, and now began to touch JS, just began to have a inexplicable excitement, until the touch of a few days after the total feeling a little overwhelmed.

JavaScript is a scripting language that has been renamed by LiveScript, possibly to better promote the scripting language (leveraging the Java language's popularity), so Netscape company in the last
It is decided to rename it to JavaScript, but it doesn't really matter to Java. JavaScript is a client-based browser (and now has server-side, such as Nodejs), based on objects, things
-Driven scripting language. JavaScript also has a cross-platform feature. As with all scripting languages, JavaScript is interpreted dynamically for execution.
JavaScript was invented by Netscape and finally handed over to the European Computer Manufacturers Association (ECMA), and the ECMA standardized JavaScript. Before you have JavaScript, the internet page
Face is static content, like a piece of paper full of content, Netscape company in order to enrich the Internet function, so in the browser to extend the JavaScript support, so that greatly expanded the Internet page
Features, so that the Internet can have colorful animations and user interaction, so its code is usually embedded in HTML pages.

Basic features of javascript:
interpreted scripting language
The program does not need to be compiled.
The runtime is translated into machine language.
Each time it is executed, it must be translated once. So it's inefficient and relies on the interpreter (like Google's V8 engine), but it's cross-platform (supported under a variety of browsers, and
Can be run on every operating system, such as Windows, Linux, etc.).
Weakly typed language, whose variables are not declared before they are used, and are checked by the interpreter at run time for their data types.
* Compiled language:: The program needs a special compilation process before execution, the program is compiled into machine language files, the runtime does not need to re-translate, directly use the results of the compilation.
The program executes efficiently and relies on the compiler for poor cross-platform performance. such as C, C + + and so on.
In addition, programming languages are divided into two types, declarative programming and imperative programming.
We can define the difference between them as follows:
Imperative programming: Command "Machine" How to do things, so whatever you want, it will follow your command. For example, common imperative programming languages are:
Java, C, C + +, JavaScript, PHP, and so on.
Declarative programming: Tell the Machine what you want (what), and let the machine figure out how to do it. For example, common declarative languages are: CSS, SQL.

2.2 JavaScript Run
2.2.1 Running JavaScript
1. Use the javascript: prefix to build URLs that execute JavaScript code.
<a href= "Javascript:alert (' Run js! ‘);" ></a>
<form action= "Javascript:alert (' Run js! ‘);" ></form>
2. Bind the event to run the JS code when the event is triggered.
<button onclick= "alert (' Run js! ') ' ><button/>
3. Execute the JS code in the script element.
<script type= "Text/javascript" >
Alert ("Run js! ");
4. Introduce an external JS file.
<script type= "Text/javascript" src= "JS file is located in the URL path" ></script>
2.2.2 Hello World
1. Output Hello world.
<!--pop-up in the browser
<script type= "Text/javascript" >
Alert ("Hello world!");
<!--output in console
<script type= "Text/javascript" >
Console.info ("Hello world!");
<script type= "Text/javascript" >
var data = prompt ("Please enter data:");
Console.info (data);
</script>2.3 JavaScript syntax
2.3.1 Notes
JS supports annotations in two formats. The text after "//" at the end of the line is ignored by JS as a comment. In addition, the text between "/*" and "* *" is also used as a comment, which can be written across lines, without
can have nested annotations.
The following comments are valid:
Here is a single-line comment
/* This is also a single comment */
Here is
Multi-line comments
2.3.2 Keywords
JS takes some identifiers and uses them as their own keywords. Therefore, it is no longer possible to use these keywords as identifiers in the program.
Break Delete function return typeof
Case do if switch var
Catch else in this void
Continue flase instanceof Throw while
Debugger finally new true with
Default for NULL try
Not used in the current version (also as a keyword), but some of the keywords that may be used in future releases
Class const enum Export Import Super
In addition, the following keywords are valid in normal JavaScript code, but are reserved words under strict mode
Implements let private public yield
Interface Package protected Static
Strict mode also restricts the use of the following identifiers, which are not entirely reserved words, but cannot be used as variable names, function names, and parameter names
Arguments eval
Some of the keywords in "Java"
Abstract double goto native static
Boolean Enum implements package Super
Byte Export Import private synchronized
char extends int protected throws
Class final interface public transient
Const float Long Short volatile
Some of the predefined "global variables" and "functions" in JS should avoid having their names used as variable names and function names.
Arguments encodeURI Infinity number REGEXP
Array encodeuricomponent isfinite Object String
Boolean Error IsNaN parsefloat syntaxerror
Date eval JSON parseint TypeError
decodeURI evalerror Math rangeerror undefined
decodeURIComponent Function NaN referenceerror urierror

2.3.3 Identifier
In JS, we need to identify a number of elements in the code, including function names, variable names, and so on.
The name we choose is called an identifier and must follow the following rules: identifiers cannot use keywords
Identifiers can contain letters, numbers 0-9, underscores (_), or dollar signs ($).
The first character of an identifier must be a letter, underscore, or dollar sign.
Identifiers are case-sensitive and do not specify a maximum length.
The following identifiers are valid:
Identifier USERNAME1 user_name _sys_var1 $change Public
The following identifiers are not valid:
1_name *system Public
2.3.4 Direct quantities and variables
1. Direct volume
"Hello World"//String
True false//Boolean value
/regex///Regular expression
2. Variables
var x = 1;//The value of the variable x is a digital direct quantity of 1
Equivalent to the inside of mathematics, set x=1.
2.3.5 statements
var num = 1;//declaration statement
if (num = = 1) {//if judgment statement
Alert (1);

There are two types of JS

1. Basic type:

Number type: contains integers and floating-point numbers (that is, decimals).
Boolean type: Only True and false two values.
String value must be enclosed in quotation marks, which can be single or double quotation marks.
Undefined type: specifically used to determine a variable that has been created but has no initial value.
Null type: Used to indicate that a variable value is empty.

Reference type:

Native object: The so-called native object is an object that is provided by JavaScript, independent of the hosting environment (ie, the browser), which is simply the object defined in the ECMA-262 standard. It packs
Includes: Object, Function, Array, String, Boolean, number, Date, REGEXP, Error, Evalerror, Rangeerror, Referenceerror,
SyntaxError, TypeError, Urierror, Arguments, JSON.
Built-in objects: Built-in class object JavaScript is provided, independent of the hosting environment objects, that already exist when the JavaScript program executes. Built-in objects are actually native objects
, but unlike the native object, the built-in object does not need to be explicitly initialized because it is already initialized. Only two built-in objects are defined in ECMA-262: Global and
Host object: A host object refers to an object that is related to the host environment, the browser. All BOM and Dom objects are host objects.

Data type conversions:

Auto-Convert and cast

ToString () function
The ToString () method is from the native object, because all objects inherit from object, so all objects have the ToString () method. In addition, because of the Boolean, number, and string three
A primitive type can be seen as a Boolean, number, and string of reference types, so you can also use ToString ().
Note: null and undefined do not have ToString ().

, the ToString () method of number type is special, and it has two modes, the default mode and the base mode. With the default mode, the ToString () method simply applies the string output value.

2. Convert to Digital
Parseint and Parsefloat are two methods from the built-in object global, and number is derived from the native object number.
parseint () function
The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns NaN, and no further action is taken. But if the character is a valid number
Word, the method will look at the character at position 1 and perform the same test. This process continues until a character that is not a valid number is found, and parseint () converts the string before the character
into numbers.
For example:
var num = parseint ("12345px");//Return 12345
var num1 = parseint ("0xA"); 10
var num2 = parseint ("56.9"); 56
var num3 = parseint ("Red"); NaN
Base mode
var num4 = parseint ("10", 8); 8
Parsefloat () function
The Parsefloat () method is similar to the parseint () method, viewing each character starting at position 0 until the first non-valid character is found, and then the string that precedes the character is transferred
Change to an integer. However, for this method, the first decimal point that appears is a valid character. If there are two decimal points, the second decimal point will be considered invalid. Parsefloat () will take this
The character before the decimal point is converted to a number. This means that the string "11.22.33" will be parsed into 11.22. Another difference between using the Parsefloat () method is that the string must be in decimal form
Represents a floating-point number instead of octal or hexadecimal. The method ignores the leading 0, so the octal number 0102 is parsed to 102. For hexadecimal number 0xA, the method returns NaN,
Because in floating-point numbers, X is not a valid character. (Note: After testing, the specific browser implementation returns 0, not NaN.) )
var fNum1 = parsefloat ("12345red"); Returns 12345
var fNum2 = parsefloat ("0xA"); Return NaN
var fNum3 = parsefloat ("11.2"); Returns 11.2var fNum4 = parsefloat ("11.22.33"); Returns 11.22
var fNum5 = parsefloat ("0102"); Returns 102
var fNum1 = parsefloat ("Red"); Return NaN
Number () function
The coercion type conversion of the number () function is similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial value. Do you remember, parseint () and
The Parsefloat () method converts only the string before the first invalid character, so "1.2.3" is converted to "1" and "1.2" respectively. Coercion type conversion with number (), "1.2.3" will return
NaN because the entire string value cannot be converted to a number. If the string value can be fully converted, number () will determine whether to call the parseint () method or the Parsefloat () method.
Number (false); 0
Number (true); 1
Number (undefined); 0
Number (NULL); 0
Number ("1.2"); 1.2
Number ("12"); 12
Number ("1.2.3"); NaN
Number (new object ()); NaN
Number (false); 0

1. Conditional operator (Trinocular operation)
The syntax format for the trinocular operator is as follows:
(expression)? If-true-statement:if-false-statement;
The operation rule of the trinocular operator is to evaluate the logical expression expression first, and if the logical expression returns True, the second part of the statement is executed, and if the logical expression returns FALSE, the
Three-part statement.
For example:
5 > 3? Alert ("5 greater than 3"): Alert ("5 less than 3");

JS Getting started with heart knot

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.

Tags Index: