Basic knowledge and functions of JavaScript Distilled

Source: Internet
Author: User

I. Operators
Copy codeThe Code is as follows:
. [] () Attribute access and function call
Delete new typeof + -! Unary operator
*/% Multiplication, division, modulo
+-Addition, connection, and Subtraction
>=<=> <Inequality operator
===! = Equality operator
& Logic and
| Logical or
? : Ternary Operator


Ii. Statements:
1. if statement
Copy codeThe Code is as follows:
Var dayOfWeek = 0;
If (day = 6 ){
}
Else if (dayOfWeek === 0 ){
}
Else {
}

2. switch statement
Copy codeThe Code is as follows:
Var dayOfWeek = 0;
Switch (dayOfWeek ){
Case 6:
Break;
Case 0:
Break;
Default:
Break;
}

The switch statement precisely matches the value of the switch expression with all specified Case expressions (= ). When a match is found, the statement in the matched case clause is executed. If no matching is found, the optional default statement is executed.
3. while statement
Copy codeThe Code is as follows:
Var count = 0;
While (count <= 10 ){
Count ++;
}

4. do/while statement
Copy codeThe Code is as follows:
Var count = 0;
Do {
Count ++;
} While (count <= 10 );

5. for statement
Copy codeThe Code is as follows:
For (var count = 0; count <= 10; count ++ ){
}

6. for/in statements
Copy codeThe Code is as follows:
Var colors = ["Red", "Yellow", "Blue"];
For (var color in colors ){
}

7. try/catch statements
Copy codeThe Code is as follows:
Try {
}
Catch (e ){
}

8. throw statement
Copy codeThe Code is as follows:
Throw {
Name: 'argumentoutofrangeerror ',
Message: 'year must> 0'
}

9. return Statement
Return "Red ";
The return Statement returns a function in advance. It can also specify the value to be returned. If no return expression is specified, the value is undefined.

Iii. Type:
1. Number
Var value = 1.0;
JavaScript has only one numeric type. It is internally represented as a 64-bit floating point number.
Special Value:
Special values of NaN non-Numbers
Special values of Infinity
2. String
Var value = "One ";
A JavaScript string is a sequence of 16-bit Unicode characters. Strings can be enclosed in single quotes or double quotation marks.
String escape:
\"\'\\
\ B Return character
\ F page feed
\ N linefeed
\ R carriage return
\ T Tab
\ UXXXX Unicode characters specified by 4-digit hexadecimal XXXX
3. Boolean Value
Var value = true;
Boolean values have only two values: true and false.
The following values are treated as false values:
False
Null
Undefined
Null String''
Number 0
Number NaN
4. null
Var value = null; // null
In JavaScript, null is a special value, indicating "no value ".
5. undefined
Var value; // undefined
Undefined is a special value in JavaScript. Undefined is returned when an undeclared variable, a declared but not assigned value, or an object attribute does not exist.
6. Object
Copy codeThe Code is as follows:
Var car = {
Brand: "Honda ",
Color: "Red ",
Run: function (){
// Run
}
};
Var value = car. brand;

Objects in JavaScript are variable key-Value Sets. An object is a property container. Each property has a name and a value. The attribute name can be any string including an empty string. The attribute value can be any value except the undefined value.
Copy codeThe Code is as follows:
Car. prototype. stop = function (){
// Stop
};
Car. run ();

Each object is connected to a prototype object and can inherit attributes from it. The prototype connection does not work when it is updated. It is used only when the value is retrieved.
Modularization:
Copy codeThe Code is as follows:
Var App = {};
App. employee = {
Name: "Joyce"
}
App. customer = {
Name: "Jason"
}

7. Functions
Copy codeThe Code is as follows:
Var add = function (a, B ){
Return a + B;
};
Add (1 + 2 );

In JavaScript, functions are objects. Functions can be defined in other functions. An internal function can access parameters and variables of external functions that enclose it. This is called a closure.
Closure:
Copy codeThe Code is as follows:
Var car = function (brand, color ){
Return {
GetBrand: function (){
Return brand;
},
GetColor: function (){
Return color;
}
};
};
Var myCar = car ("Honda", "Red ");
MyCar. getBrand ();

Function inheritance:
Copy codeThe Code is as follows:
Var mammal = function (spec ){
Var that = {};
That. getName = function (){
Return spec. name;
};
That. says = function (){
Return spec. saying | '';
};
Return that;
};
Var myMammal = mammal ({name: 'herb '});
Var cat = function (spec ){
Spec. saying = spec. saying | 'meow ';
Var that = mammal (spec );
That. purr = function (n ){
// Purr
};
That. getName = function (){
Return that. says () + ''+ spec. name +'' + that. says ();
};
Return that;
};
Var myCat = cat ({name: 'henrietta '});

8. Array
Var colors = ["Red", "Yellow", "Blue"];
Var value = colors [0]; // "Red"
An array is a set of key values like an object. The difference is that the array can use an integer as the attribute name. Array also provides a very useful set of built-in methods.
Each array has a length attribute. The value of the length attribute is the maximum integer property name of the array plus 1. It is not necessarily equal to the number of attributes in the array.
9. Regular Expression
Copy codeThe Code is as follows:
Var numberRegex =/^ -? \ D + (? : \. \ D *)? (? : E [+ \-]? \ D + )? $/I;
NumberRegex. test (1.2); // true

Regular Expression grouping:
() Capturing Group
(? :) Non-capturing Group
Regular Expression escape:
\\\/\ [\] \ (\) \{\} \? \ + \ * \ | \. \ ^ \ $
\ F page feed
\ N linefeed
\ R carriage return
\ T Tab
\ UXXXX Unicode characters specified by 4-digit hexadecimal XXXX
\ D matches a number (equivalent to [0-9])
Reference of capture type group 1 (\ 2 and so on)
Regular Expression class escape:
\-\/\ [\] \ ^
\ B Return character
\ F page feed
\ N linefeed
\ R carriage return
\ T Tab
\ UXXXX Unicode characters specified by 4-digit hexadecimal XXXX
\ D matches a number (equivalent to [0-9])
Regular Expression quantifiers:
? Match 0 or 1 time (same as {0, 1 })
* Match 0 or multiple times (same as {0 ,})
+ Match 1 or multiple times (same as {1 ,})
{N} matches n times
{N,} matches at least n times
{N, m} matches at least n times, but cannot exceed m times
Regular Expression flag:
G. perform global match (all matches)
I. Perform case-insensitive matching.
M: perform multi-row matching (^ and $ can match the row Terminator)

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.

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.