"Python"--JavaScript for web development

Source: Internet
Author: User

JavaScript

JavaScript is a programming language, the browser has built-in JavaScript interpreter, so in the browser according to the JavaScript language rules to write the corresponding code, the browser can explain and make corresponding processing.

First, the basic grammar

1. JavaScript code exists in form

<!--way one--><script type "Text/javascript" src= "js file" ></script>  <!--way two--><script Type "Text/javascript" >    JS Code content </script>

2, JavaScript code storage location

    • In the head of HTML
    • The bottom of the body code block of HTML (recommended)

Because the HTML code is executed from top to bottom, if the JS code in the head is time consuming, it will cause the user to be unable to see the page for a long time, if it is placed at the bottom of the body code block, even if the JS code is time consuming, it will not affect the user to see the page effect.

Second, the variable

The Declaration of variables in JavaScript, local variables must start with a Var, and if Var is not used, the default is to declare a global variable.

<script type= "Text/javascript" >     //global variable    name = ' Seven ';     function func () {        //local variable        var age = N;         global variable        gender = "Male"    }</script>

Code comments in javascript:

    • Single-line//
    • Multi-Line/* * *

Note: This comment takes effect only in the script block.

Third, the data type

The data types in JavaScript are divided into primitive types and object types:

    • Original type
      • Digital
      • String
      • Boolean value
    • Object type
      • Array
      • Dictionary
      • ...

Special, numeric, Boolean, null (the JavaScript language keyword, which represents a special value, commonly used to describe "null"), undefined (is a special value that indicates that the variable is undefined), and the string is immutable.

1. Numbers (number)

In JavaScript, integer values and floating-point values are not distinguished, and all numbers in JavaScript are represented by floating-point numbers.

Transformation:

    • parseint (..) Converts a value to a number, or Nan if unsuccessful
    • Parsefloat (..) Converts a value to a floating-point number, or Nan if unsuccessful

Special values:

    • NaN, not a number. Can be judged using IsNaN (num).
    • Infinity, infinitely large. Can be judged using isfinite (num).

More numerical calculations:

Constant MATH.E constant E, base of natural logarithm. The natural logarithm of the math.ln1010. The natural logarithm of the math.ln22. math.log10e the logarithm of E with a base of 10. MATH.LOG2E the logarithm of E with a base of 2. MATH.PI constant Figs/u03c0.gif. The square of the math.sqrt1_22 is eradicated with 1. The square root of the math.sqrt22. The static function Math.Abs () calculates the absolute value. Math.acos () calculates the inverse cosine value. Math.asin () calculates the inverse chord value. Math.atan () calculates the inverse tangent value. Math.atan2 () calculates the angle from the x-axis to a point. Math.ceil () rounds a number. Math.Cos () calculates the cosine value. Math.exp () calculates the exponent of E. Math.floor () to a number of people. Math.log () calculates the natural logarithm. Math.max () returns the larger of the two numbers. Math.min () returns the smaller of the two numbers. Math.pow () calculates XY. Math.random () calculates a random number. Math.Round () is rounded to the nearest integer. Math.sin () calculates the sine value. MATH.SQRT () calculates the square root. Math.tan () calculates the tangent value.

2. Strings (String)

A string is an array of characters, but in JavaScript the string is immutable: You can access text anywhere in the string, but JavaScript does not provide a way to modify the contents of a known string.

Common features:

obj.length length Obj.trim () removal               Blank Obj.trimleft () obj.trimright) Obj.charat (n) returns the nth character in a string Obj.concat (value, ...)              Stitching Obj.indexof (Substring,start) sub-sequence position Obj.lastindexof (Substring,start) sub-sequence position obj.substring (from, to)                    Get sub-sequence Obj.slice (start, end) slice obj.tolowercase () uppercase Obj.touppercase () based on index                     Lowercase obj.split (delimiter, limit) split Obj.search (regexp) matches from the beginning, returning the first position where the match succeeded (G invalid) Obj.match (regexp) Global search, if there is a G in the regular means find all, otherwise only find the first one.                                     Obj.replace (regexp, replacement) replaced, there is g in the regular replaces all, otherwise only the first match, $ number: matches the nth group content;                                     $&: The content of the current match; $ ': text that is located to the left of the matched substring; $ ': Text on the right side of the matching substring $$: Direct amount $ symbol 

3. Boolean Type (Boolean)

The Boolean type contains only true and false, unlike Python, whose first letter is lowercase.

    • = = Compare Values equal
    • ! = does not equal
    • = = = Comparison value and type are equal
    • !=== Not equal to
    • || Or
    • && and

4. Arrays

Arrays in JavaScript are similar to lists in Python

Common features:

Obj.length          the size of the array Obj.push (ele)       tail Appends the element Obj.pop ()           tail Gets an element Obj.unshift (ele) The    head insert Element Obj.shift ()         Head remove Element Obj.splice (start, DeleteCount, Value, ...)  Inserts, deletes, or replaces elements of an array                    obj.splice (n,0,val) specifies the position of the insertion element                    Obj.splice (n,1,val) specifies the position of the replacement element                    Obj.splice (n,1)     Specify location Delete element Obj.slice ()        slice obj.reverse ()      invert obj.join (Sep)       joins the array elements to construct a string Obj.concat (Val,..)  Concatenate array obj.sort ()         to sort the elements of a group

Iv. Statements and exceptions

1. Conditional statements

In JavaScript, two conditional statements are supported, respectively: if and switch,

  if (condition) {     }Elseif(condition) {             }else{     }
If statement
Switch (name) {        case ' 1 ':            = 123;               Break ;          Case ' 2 ':            = 456;              Break ;         default  :             = 777;    }
Switch Statement

2. Circular statements

JavaScript supports three loop statements, namely:

var names = ["Alex", "Tony", "Rain"for (var i=0;i<names.length;i++) {    Console.log (i);    Console.log (Names[i]);}
Way One
var names = ["Jack", "Tony", "Rain"];  for (var in names) {    console.log (index);    Console.log (Names[index]);}
Mode two
 while (condition) {    // break ;    // continue;}
Way Three

3. Exception Handling

try {    //This piece of code runs from top to bottom, where any one of the statements throws an exception the code block ends running}catch (e) {    ///If an exception is thrown in a try code block, the code in the catch code block is executed.    //e is a local variable that is used to point to an Error object or other thrown object}finally {//Whether or not the code in the     try is thrown (even if there is a return statement in the try code block), the finally code block is always executed. }

Note: Actively run out of exception throw Error (' xxxx ')

Five, function

1. Basic functions

The functions in JavaScript can basically be divided into three categories:

normal function    func (ARG) {        return true;    }          anonymous function    var func = function (arg) {        return "Tony";    }  Self-executing functions    (function (ARG) {        console.log (ARG);    }) (' 123 ')

Note: For function parameters in JavaScript, the number of actual arguments may be less than the number of formal parameters, and all actual parameters are encapsulated in the special value arguments within the function.

"Python"--JavaScript for web development

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.