2015-09-28 Seventh Lesson (JS introduction, declaring variables, data types)

Source: Internet
Author: User

vs New Step

FILE--New project--web--blank Web application-Name, location, OK-the software automatically generates a SLN file (solution), and there can be multiple projects under a solution.

Click Project-New Folder CSS and JS and image--new HTML page name Demo1

(Recommended settings: Tools--options--can be set under the font and color; text Editor----to set the number of commonly used languages uplink color hint)

What is JavaScript?

JavaScript is a scripting language, is a scripting language , simple structure , easy to use, its code can be directly placed in HTML documents , can be directly in the support of JavaScript Run in the browser . Javasript makes Web pages more interactive , more lively, and more flexible . When you do something on the Web page, an event occurs , and JavaScript writes a program that responds to the corresponding event .

1, JS Two types of annotation:

1.1. Comments that represent multiple lines : "/* * * *"

For example:

/* Function Myclick () {   alert ("This is my first JS example"); }    */

1.2, single-line comments: "//"

function Myclick () {//alert ("This is my first JS example"); //}

2, how to use JS:

First specify an event in HTML, and then write JS for this event.

For example: Set a button to name an event on the click button:

<type= "button"  value= "I am a button"  onclick= " ShowMsg () "/>

3, how to write JS:

Write JS can be written in an external JS file, you can also write directly in the head:

<type= "Text/javascript">function  showmsg () {alert (    c7>" This is my first JS example "); }</script>  

4, naming specification: in order to enhance the readability of the code, so that team members can understand, follow the naming convention.

(1) JS is case-sensitive, very strict.

(2) JS is a weak type, unified with VAR declaration variables, such as:

Shaping: Var inum=10;

Floating point: Var fnum=2.23;

Boolean: Var Bmaie=false;

function: Var fnobject=new myFunction ();

Array: var acolor=new array ();

String: var smystring= "AAAAA";
......

* Weak type and strong type:

JS is a weak type and can only be declared with Var; C # is strongly typed and declared with the corresponding type

Weak type: The way to declare a weakened type; strong type: How to declare a type of emphasis

(3) var can declare multiple variables at the same time:

var girl= "AAAA", age=19, Male=false;

(4) The JS variable does not need to be initialized, can be used later to assign value:

var couple;

(5) Variable name rule:

• The initial letter must be a letter (both uppercase and lowercase), underscore, or dollar sign.

• The remaining letters can be underscores, dollar symbols, any letters or numeric characters.

• Variable names cannot contain JS keywords.

(6) Local variables and global variables:

Global variables: Variables declared outside of {}, each function can be called;

Local variables: Variables declared within {}, only this function is available;

(7) Naming habits

Plastic I:var inum=10;

Floating point F:var fnum=2.23;

Boolean B:var bmaie=false;

function Fn:var fnobject=new myFunction ();

Array A:var acolor=new array ();

String S:var smystring= "AAAAA";

(also: Object o, Regular re, these two are not commonly used, you know)

5. Detailed Data type:

(1) Strings string:

var smystring= "Hello World";

* Supplement: Position/index: The first letter is counted from 0, followed by a space, such as "Hello World" above, the letter of position 8 is R

"Summary of property methods for strings"

Length gets the string length alert (sstring.length); The result is 11.

CharAt gets the character of the specified position, alert (Sstring.charat (8)); The result is R

Intercept alert for substring strings (sstring.substring (1)); Intercept from position 1 to the last. The result is Ello world

Alert (sstring.substring (1,10)); Intercept from position 1 to position 10 (10 not included). The result is Ello Worl

SUBSTR The Intercept alert (SSTRING.SUBSTR (1,6)) of the string; Intercept 6 bits from position 1. The result is Ello W

IndexOf search for a character position alert (Sstring.indexof ("L")); Finds the position where the first L is located, and returns 1 if none. The result is 2.

Alert (Sstring.indexof ("L", 4)); Starting at position 4, find out where the next L is located. The result is 9.

LastindexOf from the back to a character position similar to IndexOf


(2) Boolean type bool: (i.e. True or FALSE, true or false)

Var bmale=true; Declare Bmale to be true;

if (Bmale) {if Bmale is true,

Alert ("a"); then output A;
}

else{otherwise,

Alert ("B"); Then output B.

}

So the above program output a


  (3) value, and type conversion:

A) The value is added to the string:

var inum1=10;

var snum2= "20";

var snum3=inum1+snum2;

alert (SNUM3);


You can turn INum1 into a string:

var snum3=inum1.tostring () +snum2;

alert (SNUM3);


The sNum2 can also be converted into numerical values:

var inum2=inum1+parseint (sNum2);

alert (INUM2);



b) Add to the floating-point string:

var inum1=10;

var snum2= "20.123";

var snum3=inum1+snum2;

alert (SNUM3);


When the value and the string are added, turn the sNum2 into an integer:

var inum2=inum1+parseint (sNum2);

alert (INUM2);


You can also turn sNum2 into floating-point numbers:

var inum2=inum1+parsefloat (sNum2);

alert (INUM2);


c) Type conversion:

Numeric to String: Var snum1=inum1.tostring ();

String to value: var inum2=parseint (sNum2);

Turn floating point: Var inum2=parsefloat (sNum2);

The string becomes an array: var adate=smystring.split ("-"); (Var smystring= "2015-07-15";)


  (4) Arrays: storing a data set

A) How the array is declared:

1. Determine the array length: var ateam=new array (12); Array length is 12

2. Indeterminate array Length:

var acolor=new Array ();

Acolor[0]= "Blue";

acolor[1]= "Yellow";

Acolor[2]= "Green";

Acolor[3]= "BLACK";

* The above array can also be written as:

var acolor=new Array ("Blue", "yellow", "green", "Black");

This output: alert (acolor[3]);

Then get: Black


b) toString, the array becomes a string:

Alert (acolor.tostring ());

This time output: Blue,yellow,green,black


c) Join, change the connector:

Alert (Acolor.join ("-"). ToString ());

This time output: Blue-yellow-green-black


d) Split, turn the string into an array:

var smystring= "2015-07-15";

var adate=smystring.split ("-");

Alert (adate[2]);

At this point the output is: 15


e) Reverse the array elements to appear in reverse order:

var acolor=new Array ("Blue", "yellow", "green", "Black");

Alert (Acolor.reverse (). toString ());

At this point the output is: Black,green,yellow,blue


f) Sort, which sorts the array elements according to A-Z order:

var acolor=new Array ("Blue", "yellow", "green", "Black");

Alert (Acolor.sort (). toString ());

At this point the output is: Black,blue,green,yellow

2015-09-28 Seventh Lesson (JS introduction, declaring variables, data types)

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.