Chila javascript< 13 >

Source: Internet
Author: User

First, JavaScript

Syntax: Case sensitive, weak type (all types are guided and declared with Var)

Written in the <script></script> label can not be placed in the title

var num=10;

var str= ' 123a ';

var isright=true;

var ch= ' C ';

.....

Output

alert (num);

alert (str);

-When the string is in pure JS code, use double quotation marks,

When strings are mixed in HTML pages, use single quotes to avoid conflicts with attribute values.

1. JavaScript events

---"OnClick mouse click event

-->onblur loses focus

-->onmouseover Mouse Hover

-Because the type in JS is a weak type, it is very easy to calculate the number when it becomes the case of string concatenation

parseint (string);

Convert a string to a number for calculation

2. Anonymous functions (similar to anonymous methods for C # delegates)

Use a method as a variable

var func=function () {var sum=0;  for (var i=1; i<=; i++) {sum+ =i;} return sum;} var num=func (); alert (num);

3. Objects

The object in JS can be seen as a constructor

When creating an object, you can create an object member directly from the name of the object, as long as it is assigned once and can be used directly as a property or method of an object.

function Person () { This. name="Zhang San"; This. age= +; This. gender='male'; This. sayhello=function () {alert ('Hello, my name is'+ This. name+', this year'+ This. age+'years old,'+'I was'+ This. gender+'classmate. ');//alert (' Hello, my name {0}, this year {1} years old, I am {2} classmates. ', this.name,this.age,this.gender);}}varp=NewPerson ();p. Name='Chiang Kai is';p. Zhiye='Student';p. Sayhi=function () {alert ('Hello, my name is'+ This. name+', this year'+ This. age+'years old,'+'I was'+ This. gender+'classmate. I was'+ This. Zhiye);} P.sayhello ();p. Sayhi ();
JS Array

Push method
Appends one or more new elements to the end of the array and returns the new length value of the array.

Arr.push ([Item1 [item2 [...] [Itemn]])

Such as:

var New Array (0,1,2,3,4); Arr.push (5,6, 7,8, "Maple Rock", "Cnlei"); alert (Arr[arr.length-1]);

————————————————

Pop method
Moves the last element in the array and returns the element, and returns undefined if it is empty.

Arr.pop ()
Example
var arrayobj = new Array (0,1,2,3,4);
Alert (Arrayobj.pop ());

Shift method
Moves the first element in an array and returns the element.
Arr.shift ()

Unshift method
Inserts the specified element or elements into the starting position of the array and returns the array.
Arr.unshift ([item1[, item2 [, ...] [, Itemn]]])

Concat Method (Array)
Returns a new array, which is a combination of two or more arrays.
Array1.concat ([item1[, item2[, ... [, Itemn]]])

The Concat method returns an Array object that contains the connection for array1 and any other items provided.
Items to add (item1 ... itemn) are added to the array in left-to-right order.
If an item is an array, then add its contents to the end of the array1.
If the item is not an array, it is added to the end of the array as a single array element.

The following is a copy of the element from the source array to the result array:
For the object parameters copied from the array being connected to the new array, the same object is still pointed to after replication. Any change in the new array and the source array will cause another change. For numeric values or strings connected to a new array, only its value is copied. Changing the value in one array does not affect the value in the other array.

varnew Array (1,2,3=new Array (  * =return(d);  // returns the array [1, 2, 3, "JScript", "VBScript"]}

Join method
Returns a string value that contains all the elements of an array that are concatenated together, separated by the specified delimiter.
Arr.join (char)

Arr is an array object.
Char is a String object that acts as a delimiter between array elements. If this argument is omitted, then the elements of the array are separated by a comma.

Description
If there are elements in the array that are not defined or are null, it is treated as an empty string.

Example
The following example illustrates the use of the Join method.

varnew Array (0,1,2,3,4= A.join ("-  return(b);     // returns "0-1-2-3-4″}

Sort method
Returns an Array object for which an element has been sorted.
Arr.sort (Fun)

Parameters
Arr is an Array object.
Fun is the name of the function used to determine the order of elements. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.

Description
The sort method sorts the array objects appropriately, and does not create a new array object during execution.

If you provide a fun parameter, the function must return one of the following values:
(1) A negative value, if the first argument passed is smaller than the second argument.
(2) 0 if two parameters are equal.
(3) Positive value if the first parameter is larger than the second argument.

Example

<script type= "Text/javascript" >function Ascsort (x, y) {returnx = = y?0: (x > y?)1: -1);} function Descsort (x, y) {returnx = = y?0: (x > y?-1:1);} function Randomsort (x, y) {returnMath.floor (Math.random () *2–1 );}varArray = [2,4,3,5,1,6,9,0,8];d ocument.write ("<p> Positive Order: "+ array.sort (ascsort) +" </p>");d ocument.write ("<p> Reverse: "+ array.sort (descsort) +" </p>");d ocument.write ("<p> Random Sort: "+ array.sort (randomsort) +" </p>");d ocument.write ("<p> Random Sort: "+ array.sort (randomsort) +" </p>");d ocument.write ("<p> Random Sort: "+ array.sort (randomsort) +" </p>");</script>

Slice method (Array)
Returns a segment of an array. Returns an Array object
Arr.slice (start, [end])

Parameters
The start element is a zero-based subscript.
The end element is the zero-based subscript.

The slice method has been copied to the element specified by end, but does not include the element.
If start is negative, it is processed as length + start, where length is the number of the array.
If end is negative, it is treated as a length + end, where long is the number of the array.
If End is omitted, then the slice method is copied to the end of arrayobj.
If End appears before start, no elements are copied into the new array.

Example
In the following example, all elements in the MyArray are copied to NewArray except for the last element:
NewArray = Myarray.slice (0,-1)

Splice method
Removes one or more elements from an array and, if necessary, inserts a new element at the location of the removed element, returning the element that was removed.
Arr.splice (Start, DeleteCount, [item1[, item2[, ... [, Itemn]]])

Parameters
Start specifies the starting position of the element to be removed from the array, which is calculated starting at 0.
DeleteCount the number of elements to remove.
Item1, item2,.. ., the new element to be inserted at the location of the removed element. Itemn.

Reverse method
Returns an Array object in which the element order is reversed.
Arr.reverse ()

During execution, this method does not create a new Array object.
If the array is discontinuous, the reverse method creates elements in the array to populate the interval in the array. The values for all the elements created are undefined.

Example

var // declares a variable.  New Array (0,1,2,3,4//  Create array and assign values.  //  Reverses the contents of the array.  return//  Returns an array of results. }

Chila javascript< 13 >

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.