JavaScript objects for the front-end base

Source: Internet
Author: User
Tags array sort function definition time zones local time natural logarithm square root

A. Brief introduction

 Data types other than null and undefined are defined as objects in JavaScript, and variables can be defined using the method of creating objects

Defining variables in the way that objects are created

var Aa=number.max_value;

var bb=string ("Hello World");

var cc=new Data ();

var dd=new Array ();

two. Types of Objects

1.String objects

Create: Var aa= "Hello World";

var aa=new String ("Hello World")

Properties and functions for string objects

X.length----get the length of the string X.tolowercase ()---to lowercase x.touppercase ()----to uppercase X.trim () --------String Query method X.charat (Index)----str1.charat (index); Index X.indexof (Findstr,index)----query string position x.lastindexof (findstr) x.match (regexp)----match returns an array of matching strings and returns if no match is returned NULLX.S Earch (regexp)----search returns the first character position index of the matching string example: var str1= "Welcome to the                        World of js! ";                        var str2=str1.match ("World");                        var str3=str1.search ("World");  Alert (str2[0]);     The result is "world" alert (STR3); The result is 15----substring processing method x.substr (start, length)----start represents the start position, length indicates the Intercept length x.substring (start, end)--                            --end is the end position x.slice (start, end)----the slice operation string Example: Var str1= "ABCDEFGH";  var str2=str1.slice (2,4);                          var str3=str1.slice (4);                            var str4=str1.slice (2,-1);                            var str5=str1.slice ( -3,-1); alert (STR2); The result is "CD" Alert (STR3); The result is "EFGH" alert (STR4); The result is "CDEFG" alert (STR5);                 The result is "FG" X.replace (FINDSTR,TOSTR)---string substitution x.split ();                                 -----split string var str1= "One, two, three, four, five, six, day";                                var strarray=str1.split (","); Alert (strarray[1]);//result is "two" X.concat (ADDSTR)-----stitching string
  2.Array Objects

To create an object:

Creation method One: Var aa= new Array ("Hello World");

Creation method Two: Var bb=[1,2,3, "logo"];

Create method Three: Var cc=new Array ();

cc[0]=7;

cc[1]=9;

cc[2]=10;

cc[3]= "Hello World";

To create a two-dimensional array:

var cnweek=new Array (7); for (var i=0;i<=6; i++) {    cnweek[i]   Array (2);} Cnweek[0][0]= "Sunday"; cnweek[0][1]= "Sunday"; cnweek[1][0]= "Monday"  ; cnweek[1][1]= "Monday"; cnweek[6][0]= "Saturday"; cnweek[6][ 1]= "Saturday";

1. Properties and methods of array objects

  Join:

var aa=[1,2,3,4];

var aaa=aa.join ("-");

Alert (AAA);

Contact

var aa=[1,2,3,4];

var bb=aa.contact (4,5);

Alert (aa.tostring ()); 1,2,3,4

Alert (bb.tostring ()); 1,2,3,4,5

Array sort: Reverse sort

//x.reverse ()//x.sort () var arr1=[32, N, 111, 444];//var arr1=["a", "D", "F", "C"];arr1.reverse ();//reverse array element alert ( Arr1.tostring ());//The result is 444,111,12,32arr1.sort ();    Sort array Element alert (arr1.tostring ());//result for 111,12,32,444//------------------------------Arr=[1,5,2,100];//arr.sort () //alert (arr);//If you want to compare by numbers? Function Intsort (A, b) {    if (a>b) {        return 1;//-1    } else if    (a  <B) {        return-1;//1    }    else {        return 0    }}arr.sort (intsort); alert (arr); function Intsort (A, b) {return a-a    ;}

 Array slicing operations

var aa=[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ']var aa1=aa.slice (2,4); var aa2=aa.slice (4); var aa3=aa.slice (2,-1); alert ( Aa2.tostring ());  ' C,d ' alert (aa3.tospring ());  ' E,f,g,h ' alert (aa4.tospring ());  ' C,d,e,f,g '

  The push and pop of an array

var arr1=[1,2,3];arr1.push (4,5); alert (arr1);//result = "1,2,3,4,5" Arr1.push ([6,7]); alert (ARR1)//result = "1,2,3,4,5,6,7" Arr1.pop (); alert (arr1);//The result is "1,2,3,4,5"

  The shift and unshift of the array

var arr1=[1,2,3];arr1.unshift (4,5); alert (arr1);  The result is "4,5,1,2,3" arr1. Unshift ([6,7]); alert (arr1);  The result is "6,7,4,5,1,2,3" Arr1.shift (); alert (arr1);  The result is "4,5,1,2,3"

Summarize JS Array features

The attribute of the array in JS//java,  specifies what type of array it is, and only what type it is. There is only one type. Arrays in//js in 1:js can be any type, without any restrictions. An array in an array attribute 2:js in//js, The length is changed with the subscript. How long it will take.            

 4.DATE objects

Creating objects

var nowd1=new date (); Alert (nowd1.tolocalestring ());//Method 2: Parameter is a date string var nowd2=new date ("2004/3/20 11:12"); Alert ( Nowd2.tolocalestring ()); var nowd3=new Date ("04/03/20 11:12"); Alert (nowd3.tolocalestring ()); Method 3: parameter is the number of milliseconds Var nowd3=new Date (nowd3.tolocalestring); alert (nowd3.toutcstring ()); Method 4: Parameter is month day hour minute second millisecond var nowd4=new date ( 2004,2,20,11,12,0,300); alert (nowd4.tolocalestring ());//milliseconds do not display directly

  Method of the Date object:

//Set Date and Time//setdate (day_of_month) Set day//setmonth (month) set monthly//setfullyear year Sethours (hour) Set hours//setminutes (minute) Set minutes//setseconds (second) Set seconds//setmillliseconds (MS) settings    Seconds (0-999)//settime (allms) sets the cumulative milliseconds (from midnight 1970/1/1) var x=new Date (); X.setfullyear (1997);        Set the year 1997x.setmonth (7);        Set the monthly 7x.setdate (1);        Set the day 1x.sethours (5);    Set Hour 5x.setminutes (12);    Set the minute 12x.setseconds (54);        Set the Seconds 54x.setmilliseconds (230); Set the millisecond 230document.write (x.tolocalestring () + "<BR>");//Return August 1, 1997 5:12 54 sec x.settime (870409430000); Set cumulative number of milliseconds document.write (x.tolocalestring () + "<BR>");//Return August 1, 1997 12:23 50 seconds

Methods for date objects (conversion of dates and times)

Conversion of date and time: getTimezoneOffset (): 8 time zones x15 degrees x4/degrees = 480; Returns the time difference between local and GMT, in minutes toutcstring () returns the international Standard Time string tolocalstring () Returns the local format time string date.parse (x) returns the cumulative number of milliseconds (from midnight 1970/1/1 to local time) DATE.UTC (x) returns the cumulative number of milliseconds (from midnight 1970/1/1 to International time)
   5.Math Objects
#math对象的方法abs (x)    the absolute value of the return number. EXP (x)    returns the exponent of E. Floor (x) is rounded down with a logarithmic. Log (x)    returns the natural logarithm of the number (bottom is e). Max (x, y)    returns the highest value in X and Y. Min (x, y)    returns the lowest value in X and Y. The POW (x, y)    returns the Y power of X. Random ()    returns an arbitrary number between 0 and 1. Round (x)    rounds the number to the nearest integer. Sin (x)    returns the sine of the number. sqrt (x)    returns the square root of the number. Tan (x)    returns the tangent of the angle.

6.function objects

function definition:

Function name (parameter)

function body

Return

Attention:

JS load function execution is different from Python, it is executed after the whole load is finished, so the execution function is placed above and below the function declaration can be

Properties of a Function object

Aler (func1.length)//Return function expected number of arguments

Call to function:

As long as the function name is written, the parameter does not write or error

The built-in object for the function arguments

function Add (A, b) {        console.log (a+b);//3        Console.log (arguments.length);//2        console.log (arguments);// [+]    }    Add (arguments)    ------------------use 1------------------    function Nxadd () {        var result=0;        for (var num in arguments) {            result+=arguments[num]        }        alert (Result)    }    Nxadd (1,2,3,4,5)//     ------------------The usefulness of arguments 2------------------    function f (a,b,c) {        if (arguments.length!=3) {            throw new Error ("function f called with" +arguments.length+ "arguments,but it just need 3 arguments")        }        else {            alert ("success!")        }    }    F (1,2,3,4,5)
  7. Anonymous functions
anonymous function    var func = function (arg) {        return "Tony";    } Application of anonymous functions    (function () {        alert ("Tony");    }) ()    (function (ARG) {        console.log (ARG);    }) (' 123 ')

JavaScript objects for the front-end base

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.