0530JavaScript Basic 2

Source: Internet
Author: User
Tags floor function local time naming convention object model

common built-in objects

The so-called built-in objects are some of the objects that ECMAScript provides, and we know that objects have corresponding properties and methods

Arrays Array (partially equivalent to list)

1. How arrays are created

var colors = [' red ', ' color ', ' yellow '];

Create an object using the New keyword to create the constructor using the constructor (which is spoken later)

New Array ();

2. Assigning values to arrays

var arr = [];  one by one assignment by subscript arr[0] = 123; arr[1] = ' hahaha '; arr[2] = ' hehe hey '    

3. Common methods of arrays

3.1 Merging of arrays concat ()
var north = [' Beijing ', ' Shandong ', ' Tianjin ']; var south = [' Dongguan ', ' Shenzhen ', ' Shanghai ']; var newcity = North.concat (south); Console.log (newcity)
3.2 Join () joins the elements in the array using the specified string, which forms a new string
var score = [98,78,76,100,0]; var str = score.join (' | ') ); Console.log (str); //98|78|76|100|0
3.3 Converting an array to a string toString ()
var score = [98,78,76,100,0]; toString () is converted directly to a string between each element using a comma separated by var str = score.tostring (); Console.log (str);// 98,78,76,100,0
3.4 Slice (start,end); Returns a section of an array, left-closed, right-open
var arr = [' Zhang San ', ' John Doe ', ' Wang Wen ', ' Zhao Liu ']; var newArr = Arr.slice (1,3); Console.log (NEWARR); ["John Doe", "Wang Wen"]
3.5 The last element of a pop migration divisor group
var arr = [' Zhang San ', ' John Doe ', ' Wang Wen ', ' Zhao Liu ']; var newArr = arr.pop (); Console.log (NEWARR); ["Zhang San", "John Doe", "Wang Wen"]
 3.6 push () add an element to the array at the end of 
Var arr = [' Zhang San ', ' John Doe ', ' Wang Wen ', ' Zhao Liu ' ]; var newArr = Arr.push (' pony '); Console.log (NEWARR); ["Zhang San", "John Doe", "Wang Wen", "Zhao Liu", "Pony"]
3.7 reverse () Flip array
var names = [' Alex ', ' Xiaoma ', ' tanhuang ', ' angle ' "; // 4. Invert array names.reverse (); Console.log (names);
3.8 Sort Array Sorting
var names = [' Alex ', ' Xiaoma ', ' Tanhuang ', ' Abngel '];names.sort (); Console.log (names);//["Alex", "angle", "Tanhuang "," Xiaoma "]

3.9 Determine if an array: IsArray ()

Boolean Type value = Array.isarray (detected value);
Strings string

4.1 Chartat () returns the character of the position of the specified index

var str = ' Alex ';  var charset = Str.charat (1); Console.log (charset);  l    

4.2 Concat Returns a String value that represents the concatenation of two or more strings

var str1 = ' al ';  var str2  = ' ex '; Console.log (Str1.concat (STR2,STR2));  alexex    

4.3 replace (b) replaces string A with string b

var a = ' 1234567755 ';  var newstr = a.replace ("4567", "* * *"); Console.log (NEWSTR);  123****755    

4.4 indexof () looks for the subscript of the character, and returns 1 if it finds the subscript of the returned string. Same as Seach () method usage

var str = ' Alex '; Console.log (Str.indexof (' e '));  2console.log (Str.indexof (' P ')); //-1    

4.5 Slice (start,end) left close right open split string

var str = ' pony brother '; Console.log (str.slice);  Horse  

4.6 Split (' A ', 1) splits the string with string A and returns a new array. If the second argument is not written, the entire array is returned, and if the number is defined, the maximum length of the array is returned

var  str =  ' My goodness, a yes, what are you talking about? ' a ha ha '; Console.log (Str.split (' a '));  ["My God,", "Yes, what are you talking about?", "hahaha"]  

4.7 substr (Statr,end) left closed right open

var  str =  ' My goodness, a yes, what are you talking about? ' a hahaha '; Console.log (Str.substr (0,4));  Oh, my God!  

4.8 tolowercase () Turn lowercase

var str = ' xiaomage '; Console.log (Str.tolowercase ());//xiaomage

4.9 touppercase () Turn capital

var str = ' xiaomage '; Console.log (Str.touppercase ());

Special:

1. Convert number type to string type var num = 132.32522;  var numstr = num.tostring () console.log (typeof numstr)     
rounding var newnum = num.tofixed (2) Console.log (newnum)  
Date Day Object

Create Date object only constructor one way, use the New keyword

a Date object is created with a new date (); 

Create Date Object
var mydate=new Date ();

Get a day in one months
Console.log (Mydate.getdate ());

Return local time
Console.log (MyDate (). tolocalstring ());//2018/5/27 10:36:23

Math Built-in objects

6.1 Math.ceil () rounding up, ' ceiling function '

var x = 1.234;  The ceiling function represents a value  greater than or equal to X, and the nearest integer to it is 2var a = Math.ceil (x); Console.log (a);//2    

6.2 Math.floor down rounding, ' floor function '

var x = 1.234;  less than or equal to X, and the nearest integer to it is 1var b = Math.floor (x); Console.log (b);  1      

6.3 Maximum and minimum values for two numbers

the minimum value of the maximum value of two console.log (Math.max (2,5)) is obtained. //5console.log (math.min (2,5));  2    

6.4 Random number Math.random ()

var ran = math.random (); Console.log (ran); [ 0,1)

What do you do if you take a random number between 100-200?

Back over formula: Random number between Min-max: min+math.random () * (max-min)

Function

Function: is to encapsulate some statements, and then execute them in the form of calls .

Role of the function:

    • Write a large number of repeated statements in the function, and later when you need these statements, you can call the function directly, avoid duplication of work.

    • Simplify programming and make programming modular.

Console.log ("Hello World");    SayHello ();      // calling functions    // To define a function:    function SayHello () {        console.log ("Hello");        Console.log ("Hello World");    }
First step: definition of function

Syntax for function definitions:

function name () {    }

The explanations are as follows:

    • Function: is a keyword. Chinese is "function", "function".

    • Function name: The naming convention is the same as the naming of variables. Can only be letters, numbers, underscores, dollar signs, and cannot start with a number.

    • Parameters: There is a pair of parentheses behind, which is used for parameters.

    • Inside the curly braces is the statement of this function.

Step Two: Call the function

Syntax for function calls:

function name ();
Parameters of the function: formal parameters and arguments

Parameters of a function include formal and actual arguments

Note: The number of actual and formal parameters should be the same.

SUM (3,4);        Sum ("3", 4);        Sum ("Hello", "World");         // functions: Summing        function sum (A, b) {            + b);        }
return value of the function
Console.log (SUM (3, 4));         // functions: Summing        function sum (A, b) {            return a + b;        }
pseudo-Array arguments

Arguments represents the actual argument. There is a particular place to be: arguments is only used in functions .

(1) Number of return function arguments : arguments.length

Example:

    FN (2,4);    fn (2,4,6);    fn (2,4,6,8);    function fn (a,b,c) {        console.log (arguments);        Console.log (fn.length);         ////  Get the number of arguments console.log ("----------------");}        

Results:

(2) The reason is that arguments is a pseudo-array, because:arguments can modify the element, but cannot change the length of the array . Example:

    FN (2,4);    fn (2,4,6);    fn (2,4,6,8);    function fn (A, b) {        arguments[0] =;  ////  This method does not pass because the element cannot be added}       

There are several ways to empty an array:

   var array = [1,2,3,4,5,6];    Array.splice (0);      // mode 1: Delete all items in the array    array.length = 0;     ////  mode 3: Recommended     
event operations on the DOMFirst, the composition of JavaScript

The JavaScript base is divided into three parts:

    • The grammatical standard of ecmascript:javascript. Includes variables, expressions, operators, functions, if statements, for statements, and so on.

    • DOM: The Document Object model, an API that operates on elements on a Web page . For example, let the box move, color change, Carousel diagram and so on.

    • BOM: Browser object model, API for manipulating browser-part functionality . For example, let the browser automatically scroll.

II. Events
JS is a language with event-driven core.
Three elements of an event

three elements of an event: Event source, event, event driver .

For example, I use my hand to press the switch and the light is on. In this matter, the source of the event is: hand. The event is: press the switch. Event drivers are: lights on and off.

Another example, the page pops up an ad, I click on the upper right corner X , the ad is closed. In this matter, the source of the event is: X . The event is: onclick. The event driver is: The ad is off.

So we can sum up: Who is the next event, who is the source of the event.

Summarized as follows:

    • Event Source: The HTML tag that raised the subsequent event.

    • Event: JS is already defined (see).

    • Event drivers: Actions on styles and HTML. That is, Dom.

The code writing steps are as follows:(important)

    • (1) Get event Source: document.getElementById ("box"); Similar to the iOS language UIButton *adbtn = [UIButton buttonwithtype:uibuttontypecustom];

    • (2) Bind event: Event Source box. Event onclick = function () {event driver};

    • (3) Writing event drivers: about DOM operations

0530JavaScript Basic 2

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.