Java JavaScript (i)

Source: Internet
Author: User

1.JavaScript

A. Definition: JavaScript is a scripting language and a lightweight programming language

B. Implementation: ① directly through the Onxx attribute inside the tag to drive JS execution

<type= "button"  value= "Test"  onclick= "alert (' Hello ') ">

② introduction of external JS files--javascript file

<src= "My.js"></script>

③ can be placed in the <body> and

<type= "Text/javascript">    alert ("  Script"); </ Script >

2. Data type:

A. Data type: String (string), Number, Boolean (Boolean), arrays (array), objects (object), Null, Undefined

var num = 1; var str = "Hello"; var true ; var str1;

Note: The Var declaration variable is represented by a local variable, and the non-additive represents a global variable.

B. You can convert to string and Boolean by using new number:

var New Number (1); var New Number (true);    // result is 1 var New Number ("n");    // if the string is not a number, the result is Nan

C. You can convert to numeric and Boolean by using the new String:

var New String ("Hello"); var New String (n); var New String (ture);    // result is true

D. You can convert numbers and strings by using the new Boolean:

var New Boolean ("123");    // non-null is true var New Boolean (2);    // non-0 is true

Note: string: "" Null is false, non-NULL is True

Number: Not 0 is true,0 to False

The difference between e.null and undefined:

①undefined: Declares that the variable is not initialized

②null: When this element is not found in the document

F. Objects (object):

① initialization:

// Object Initialization method one: var person = {    userName:"Zhang San",    userpwd:"123",    userage: // Object Initialization mode two: var New  = "John Doe"= 18;

② Property Access:

// object's property access method one:    object name. property name; alert (user.username); // How to access the property of an object two:    object name ["Property name"]; Alert (user["attribute name"]);

G. Arrays

Statement:

var New Array ();    // declaration Method One var New Array (3); var nums = [1, 2, "Zhang San"];    // declaration Method Two var New Array ("Zhang San", "John Doe");    // declaration Method Three var New Array (new number (2));

3. Operators

A. Arithmetic operator: +-*/% + +--

B. Assignment operator: + = = *=/=%=

C. comparison operators: = = = = = = = = > < >= <=! =

①===: Indicates that the type and value are the same

②==: Represents the same value

The comparison of strings in JS is usually replaced by = =. Equal

D. Logical operator:&& | | !

E. Three-mesh operator:

var num = judgment expression? True execution expression: pseudo-execution expression

4. Judgment statement: If-else switch

5. Loop statement: While Do-while for For-each

 for (i = 0; i < i++) {    alert (i);}
 for inch names) {    document.write (Names[xindex]);}

6. Methods

A. Statement:

function Test (A, b) {    // declaration method one    return a +b;}var function () {    // declaration method two }varnewfunction () {    // declaration way Three })

Note: The function default return value is undefined (undefined)

function if you need to return a value, directly return

B. Access:

Test ();

7.String (String method is similar to Java, see API)

Str.substr (1,3)    // intercept element starting with subscript 1 to fetch 3 str.substring (1,3)    // intercept 1 to 2 elements (front closed) 

Note: The length of the JS string is an attribute, while in Java it is a method

8. Time (date)

varDate =NewDate ();//StatementvarDate =NewDate ("2015/01/01"); GetDay (); //the first day of the Week (0-6)GetDate ();//Day of January (1-31)GetMonth ();//month (0-11)getFullYear ();//returns the year as a four-digit numberGetHours ();//hours (0 ~ +)Getminutes ();//minutes (0 ~ $)Getseconds ();//number of seconds (0 ~ $)Getmilliseconds ();//milliseconds (0 ~ 999)GetTime (); //returns the number of milliseconds since January 1, 1970
Date.parse ("2015/01/01");    // specify the number of milliseconds for a date

9. Math (method similar to Java, see API)

10. Global functions

IsNaN ();    // Check if a value is a number Number ();    // converts the value of an object to a number String ();    // converts the value of an object to a string parseint ();    // parses a string and returns an integer parsefloat ();    // parses a string and returns a real number eval ();    // computes the JavaScript string and executes it as script code

11. Event (events)

A.ui Related: OnLoad, onunload, OnResize, onselect

B. Focus-Related: onblur, onfocus

C. Mouse related to scroll wheel: onclick, ondblclick, onmousedown, onmousemove, onmouseout, onmouseover, onmouseup

D. Keyboard-Related: onkeydown, onkeyup, onkeypress

E. Others: Onabord, onchange

12. Regular Expressions (REXEXP)

var regex =/^[a-z]$/g;    // /pattern/attributes var New REGEXP ("[A-Z]", "G");    // New RegExp (pattern, attributes); Regex.text (str);     // retrieves the value specified in the string. Returns TRUE or Falseregex.exec (str);    // retrieves the value specified in the string. Returns the found value and determines its location

Note: Str.match (regex);

Returns the result of matching a regular expression

If the global match, the returned array

If no match succeeds, NULL is returned

Java JavaScript (i)

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.