Python development [front-end]: JavaScript, pythonjavascript

Source: Internet
Author: User
Tags constant math define function natural logarithm

Python development [front-end]: JavaScript, pythonjavascript
JavaScript is a literal-literal scripting language. It is a dynamic, weak, prototype-based language with built-in support. Its Interpreter, called the JavaScript engine, is a part of the browser and is widely used in the script language of the client. It was first used on HTML (an application under the standard General Markup Language) web pages, it is used to add dynamic functions to HTML webpages. In 1995, Brendan Eich of Netscape was first designed and implemented on the browser of Netscape Navigator. Because Netscape works with Sun, the management of Netscape wants it to look like Java, so it is named JavaScript. But in fact, its syntax style is close to Self and Scheme. To gain technical advantages, Microsoft launched JScript and CEnvi launched ScriptEase, which can run on the same browser as JavaScript. To unify specifications, JavaScript is also known as ECMAScript because it is compatible with the ECMA standard.

A Brief Introduction:

<Body> <input type = "text" id = "user"/> <input type = "button" onclick = "GETDATA (); "value =" Click me "/> <script> function GETDATA () {var I = document. getElementById ('user'); alert (I. value) ;}</script> </body>

Enter the user name in the input box, click "me", and the user name just entered will pop up in the browser window.

Two reference methods:

<Head> <meta charset = "UTF-8"> <title> Title </title> <! -- The first method is to reference the js file --> <script src = "commons. js"> </script> <! -- The second method is directly written in html --> <script> alert ('James ') </script> 

Enclose strings with quotation marks.

 

Common JavaScript Data Types

1. Variables

The declaration of variables in JavaScript is a very error-prone point. Local variables must start with a var. If var is not used, global variables are declared by default.

<Script type = "text/javascript"> // global variable name = 'James '; function func () {// local variable var age = 18; // global variable gender = "male"} </script>

2. Numbers

The integer and floating point values are not distinguished in JavaScript. All numbers in JavaScript are represented by floating point values.

Conversion:

  • ParseInt (...) converts a value to a number. If it fails, NaN
  • ParseFloat (...) converts a value to a floating point number. If it fails, NaN

Special Value:

  • NaN, not a number. You can use isNaN (num) to determine.
  • Infinity and Infinity. You can use isFinite (num) to determine
<Script> // convert a string to a number age = "18"; I = parseInt (age); </script>

More value calculation

Constant Math. E constant e, the base number of the natural logarithm. The natural logarithm of Math. LN1010. The natural logarithm of Math. LN22. Math. LOG10E is the base-10 logarithm of e. Math. LOG2E is the base-2 logarithm of e. Math. PI constant figs/U03C0.gif. The square root of Math. sqrt000022 is divided by 1. The square root of Math. SQRT22. The static function Math. abs () calculates the absolute value. Math. acos () calculates the arccosine value. Math. asin () calculates the arcsin value. Math. atan () calculates the arc 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 e index. Math. floor () for a number of people. Math. log () is used to calculate the natural logarithm. Math. max () returns a larger value of two numbers. Math. min () returns a smaller value of 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. Math
Math

3. String

A string is an array composed of characters, but in JavaScript the string is immutable: attackers can access text anywhere in the string, but JavaScript does not provide a method to modify the content of a known string.

① CharAt (enter the index position to obtain characters)

>a = 'James'<"James">a.charAt(1)<"a">a.charAt(2)<"m">a.charAt(3)<"e"

② Substring (enter the start position and end position to obtain characters)

>a = 'James'<"James">a.substring(0,2)<"Ja">a.substring(2,5)<"mes"

③ Lenght (obtain the length of the current string)

>a = 'James'<"James">a.length<5

④ Concat (String concatenation)

>a = 'James';<"James">a.concat('eric');<"Jameseric"

⑤ IndexOf (subsequence position)

>a = 'James';<"James">a.indexOf('me');<2>a.indexOf('es');<3

⑥ Split (delimiter)

>a = 'JamesaJames';<"JamesaJames">a.split('a');<["J", "mes", "J", "mes"]>a.split('a',1);<["J"]>a.split('a',3);<["J", "mes", "J"]

More:

Obj. length: obj. trim () removes the blank obj. trimLeft () obj. trimRight) obj. charAt (n) returns the nth character obj in the string. concat (value ,...) concatenate obj. indexOf (substring, start) subsequence position obj. lastIndexOf (substring, start) subsequence position obj. substring (from, to) obtains the subsequence obj Based on the index. slice (start, end) slice obj. toLowerCase () Capital obj. toUpperCase () lowercase obj. split (delimiter, limit) separates obj. search (regexp) starts from scratch and returns the first position (g is invalid) obj. match (regexp) global search. If the regular expression contains g, all are found. Otherwise, only the first is found. Obj. replace (regexp, replacement) is replaced. If there is g in the regular expression, replace all; otherwise, replace only the first match. $ number: the content of the nth group that matches; $ &: currently matched content; $ ': Text on the left of the matched substring; $': text on the right of the matched substring $: quantity directly $ symbol
More

4. Boolean Value

The boolean type only contains true and false characters. It is different from Python in lower case.

  • = Equal comparison value
  • ! = Not equal
  • === The comparison value and type are equal
  • ! === Not equal
  • | Or
  • &

5. Array

① Create an array

li = [11,22,33,44][11, 22, 33, 44]

② Insert, delete, or replace elements of an array through split

>li = [11,22,33,44]<[11, 22, 33, 44]>li.splice(1,0,99);<[]>li<[11, 99, 22, 33, 44]>li.splice(1,1,888);<[99]>li<[11, 888, 22, 33, 44]>li.splice(1,1)<[888]>li<[11, 22, 33, 44]

③ Slice slices

>li = [11,22,33,44]<[11, 22, 33, 44]>li.slice(1,3)<[22, 33]

Other common functions:

Obj. size of the length array obj. append element obj at the end of push (ele. get an element obj at the end of pop. insert the element obj in the unshift (ele) header. shift () removes the element obj from the header. splice (start, deleteCount, value ,...) insert, delete, or replace the array element obj. splice (n, 0, val) specifies the position to insert the element obj. splice (n, 1, val) specifies the position to replace the element obj. splice (n, 1) specifies the position to delete the element obj. slice () slice obj. reverse () reverse obj. join (sep) concatenates array elements to construct a string obj. concat (val ,..) connects to the array obj. sort () sorts array elements

6. Dictionary

Create a dictionary

>dic = {'k1':'v1','k2':'v2'};<Object {k1: "v1", k2: "v2"}

 

JavaScript statements and exceptions

1. Loop

JavaScript supports two loop statements: for and while.

① Method 1: for Loop. During the loop, the element of the loop is the index.

Loop Array

>   a = [11,22,33,44]    for(var item in a){        console.log(a[item])    }11223344

Loop dictionary

>   a = {'k1':'v1','k2':'v2'}    for(var item in a){        console.log(a[item])    }v1v2

② Type 2 loop: for Loop, judgment of cyclic conditions

For (var I = 0; I <10; I ++) {// loop 10 times}

Combined with array Loops

a = [11,22,33,44]    for(var i=0;i<a.length;i=i+1){        console.log(a[i])    }11223344

③ Loop 3: while LOOP, judgment of cyclic conditions

While (condition) {// break; // continue ;}

2. conditional statements

JavaScript supports two conditional statements: if and switch.

① If Condition Statement

If (condition) {} else {}

② Switch Condition Statement

Switch (name) {case '1': age = 123; break; case '2': age = 456; break; default: age = 777;} switch statement

③ ==, == ,! = ,! = Relationship between four users

1 == "1";true1 === "1";false1 != "1";false1 !== "1";true

= Match only the value, === not only the matching value but also the matching type

④ & And | or

Andif (1 = 1 | 2 = 2) or if (1 = 1 & 2 = 2)

 

JavaScript Functions

1. Basic functions

Functions in JavaScript can basically be divided into the following three types:

// Normal function func (arg) {return true;} // anonymous function var func = function (arg) {return "tony ";} // self-executed function (arg) {console. log (arg) ;}) ('20140901 ')

  

 

Other JavaScript knowledge

1. Timer

<Script> // define function func () {console. log (1); // print data on the console} // create a timer setInterval ("f1 ();", 5000 ); // 5000 indicates 5 seconds in milliseconds </script>

SetInterval ("execution function", "time") indicates creating a timer; console. log ("data") Prints input data on the console

2. marquee (scroll character)

<Body> <div id = "i1"> welcome to guide </div> <script> function func () {// obtain the content of the specified tag Based on the ID, specify the local variable var tag = document. getElementById ('i1'); // obtain the content inside the tag var content = tag. innerText; var f = content. charAt (0); var l = content. substring (1, content. length); var new_content = l + f; tag. innerText = new_content;} setInterval ('func () ', 500); </script> </body>

The final result is the Scroll display of the "Welcome to guide" character, combining string processing and Timer

 

  

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.