JavaScript for the front-end base

Source: Internet
Author: User
Tags arithmetic operators function definition logical operators natural logarithm script tag square root

JavaScript Overview of JavaScript history
    • 1992 Nombas developed the embedded scripting language for C-minus-minus (c--) (originally bundled in Cenvi software). Rename it scriptease. (The language that the client executes)
    • Netscape (Netscape) received Nombas's philosophy, (Brendan Eich) developed a set of Netscape scripting languages in its Navigator livescript 2.0 product. Sun and Netscape are done together. And then the name is JavaScript.
    • Microsoft then emulated a JavaScript clone called JScript in its IE3.0 product.
    • To unify the three, the ECMA ( European Computer Manufacturing Association) defines the ECMA-262 specification. The International Organization for Standardization (ISO/IEC) also adopted ECMAScript as the standard (iso/iec-16262). Since then, Web browsers have struggled (albeit with varying degrees of success and failure) to ECMAScript as the basis for JAVASCRIPT implementations. ECMAScript is the norm.
ECMAScript

Although ECMAScript is an important standard, it is not the only part of JavaScript, and certainly not the only one that is standardized. In fact, a complete JavaScript implementation is made up of the following 3 different parts:

    • Core (ECMAScript)
    • Document Object Model (DOM) Documents object models (consolidated js,css,html)
    • Browser object models (BOM) Broswer object Model (integrated JS and browser)
    • The vast majority of Javascript in development is object-based. It is also object-oriented.

To put it simply, ECMAScript describes the following:

    • Grammar
    • Type
    • Statement
    • Key words
    • Reserved words
    • Operator
    • Object (encapsulates inherited polymorphism) object-based language. Use an object.
how JavaScript is introducedwrite code inside the script tag
< Script >  // Write your JS code here </ Script >
Introduction of additional JS files
<src= "Myscript.js"></script>
JavaScript Language SpecificationNotes
// This is a single-line comment /* * This is a multiline comment * */
Terminator

The statements in JavaScript are terminated with a semicolon (;).

JavaScript Language BasicsVariable Declaration
    1. The variable name of JavaScript can be composed of _, number, letter, $, and cannot begin with a number.
    2. Declaring a variable using the var variable name;
var name = "Alex"; var age = 18;

Attention:

Variable names are case-sensitive.

Camel-named rules are recommended.

JavaScript data types

JavaScript has a dynamic type

var x;  // at this point x is undefined var x = 1;  // at this point x is the number var x = "Alex"  //

Number Type

JavaScript does not differentiate between integral and floating-point types, there is only one numeric type.

var a = 12.34; var b =; var c = 123e5;  // 12300000 var d = 123e-5;  // 0.00123

Common methods:

parseint ("123")  //  returnto parseint ("ABC")  //  return nanparsefloat ( "123.456")  //  return 123.456

String

var a = "Hello"var b = "world;var c = a + B; Console.log (c);  Get HelloWorld

Common methods:

Method Description
Obj.length return length
Obj.trim () Remove whitespace
Obj.trimleft () Remove the left margin
Obj.trimright () Remove the blank on the right
Obj.charat (N) Returns the nth character
Obj.concat (value, ...) Stitching
Obj.indexof (substring, start) Sub-sequence position
Obj.substring (from, to) Get sub-sequences by index
Obj.slice (start, end) Slice
Obj.tolowercase () Lowercase
Obj.touppercase () Capital
Obj.split (delimiter, limit) Segmentation

Splicing strings generally use "+"

Boolean type

The difference between python,true and false is lowercase.

var true ; var false;
Array

Similar to the list in Python.

var a = [123, "ABC"];console.log (a[1]);  // output "ABC"

Common methods:

Method Description
Obj.length The size of the array
Obj.push (Ele) Trailing append Element
Obj.pop () Gets the trailing element
Obj.unshift (Ele) Head Insert Element
Obj.shift () Removing elements from the head
Obj.slice () Slice
Obj.reverse () Reverse
Obj.join (seq) Concatenate array elements into a string
Obj.concat (Val, ...) Connection array
Obj.sort () Sort

To iterate over an element in an array:

var a = [ten, +, +];  for (var i=0;i<a.length;i++) {  console.log (i);}
Object

Similar to field data types in Python

var a = {"name": "Alex", "Age":};console.log (A.name) and Console.log (a["age"]);

Iterate through the contents of the object:

var a = {"name": "Alex", "age": +};  for (var  in a) {  console.log (i, A[i]);}
null and undefined
    • Undefined means that when the declared variable is not initialized, the default value of the variable is undefined
    • Null indicates that the value does not exist

Undefined indicates that a variable was declared, but has not been assigned a value. Null declares the variable and the variable is a null value.

Type query
typeof ("abc")  // "string"type (null)  //  "Object"typeof(true)    "boolean"typeof//  "number"
Operator arithmetic operators
+ - * / % ++ --

comparison Operators
> >= < <= = = = = = = =!==

Attention:

1 = = "1"  //  true1 = = = "1"  //  false

logical Operators
&& | | !

Assignment Operators
= += -= *= /=
Process ControlIf-else
var a = ten; if (A > 5) {  Console.log ("yes");} Else {  Console.log ("no");}

If-else If-else
var a = ten; if (A > 5) {  Console.log ("a > 5");} Else if (A < 5) {  Console.log ("A < 5");} Else {  Console.log ("a = 5");}

Switch
var New Date (). GetDay (); Switch (day) {  case 0:  console.log ("Sunday");     Break ;    Case 1:  console.log ("Monday");    Break ; default :  console.log ("...")}
While
var i = 0;  while (I < 5) {  console.log (i);  I+ +;}
Ternary operations
var a = 1; var b = 2; var c = a > B? A:b
function function definition

The functions in JavaScript are very similar to those in Python, but the way they are defined is somewhat different.

// Common function Definitions function sum (A, b) {  return a + b;} SUM (1, 2)//  anonymous functions varfunction(A, b) {    return A + b;} SUM (1, 2)//  Execute function immediately (functions (A, b) {  return A + b;}) (1, 2);

Global variables and local variables for functions

Global variables: Defined outside the function and recommended for display declarations using VAR

Local variables: defined in function content, and must be declared with Var

Scope

First find the variable inside the function, find the outer function to find, and gradually find the outermost layer, that is, the Windows object.

Common modules and methods date
varD =NewDate ();//getDate () Get Day//GetDay () Get Week//GetMonth () Get month (0-11)//getFullYear () Get the full year//getYear () get year//getHours () Get hours//getminutes () Get minutes//getseconds () Get seconds//getmilliseconds () get milliseconds//GetTime () returns the cumulative number of milliseconds (from 1970/1/1 midnight)
Json
var str1 = ' {' name ': ' Alex ', ' age ': '; var = {"Name": "Alex", "Age": Obj1}; // JSON strings converted to Objects var obj =//  object converted to JSON string var str = json.stringify (OBJ1);

Regexp
//RegExp Object    //This object is used during form validation to verify that the user-populated string conforms to the rule.    //Create regular object mode 1 parameter 1 Regular expression parameter 2 Verify mode g global/i ignores case.//Parameter 2 General fill G is OK, also has "GI".    //The first letter of the username must be in English, except for the first and only English numerals and _. The shortest length can not be less than 6 bits maximum not more than 12 bits    //----------------------------How to create 1    /*var reg1 = new RegExp ("^[a-za-z][a-za-z0-9_]{5,11}$", "G");    Verify string var str = "bc123";    Alert (Reg1.test (str));//TRUE//----------------------------creation Method 2/fill in regular expression/match mode;        var reg2 =/^[a-za-z][a-za-z0-9_]{5,11}$/g; Alert (Reg2.test (str));//True*/    //-------------------------------The method of the regular object-------------------        //The test method ==> tests whether a string is a compound regular rule. The return value is true and false.        //-------------------------4 methods that are combined with a regular in a String------------------.    //macth Search Split replace    varstr = "Hello World"; //alert (Str.match (/o/g));//Find the contents of the compound regular in the string.    //alert (Str.search (/h/g));//0 Find the content location in the string that matches the regular expression    //alert (Str.split (/o/g));//The string is cut according to a regular expression. Returns an array;Alert (Str.replace (/o/g, "s"));//Hells Wsrld the string as a regular replacement.
View Code

Math
ABS (x)    the absolute value of the return number. EXP (x)    returns the exponent of E. Floor (x) is rounded down with a logarithmic. Log (x)    returns the natural logarithm of the number (bottom is e). Max (x, y)    returns the highest value in X and Y. Min (x, y)    returns the lowest value in X and Y. The POW (x, y)    returns the Y power of X. Random number (    )between 0 and 1. Round (x)    rounds the number to the nearest integer. Sin (x)    returns the sine of the number. sqrt (x)    returns the square root of the number. Tan (x)    returns the tangent of the angle. 
Math

JavaScript for the front-end base

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.