Methods for js-objects

Source: Internet
Author: User

String Object method
var str = ' Hello ';
Console.log (str.length); 5
Console.log (Str[0]); H
STR itself holds string type data, but converts the object to a pseudo object when used
So you can reference the properties of a string object
Pseudo-object: For the basic type of data, when reading, JS will convert the data into the corresponding pseudo-object, and then read the pseudo-object (i.e.
Read out is the object type, so there are corresponding attributes).

Split is used to split a string into a string array
var str = ' Hello,world '
var result = Str.split (' l ');
Console.log (result);

var str = ' 123012301230456789 '
var result = Str.split (' 123 ');
Console.log (result);
["", "0", "0", "0456789"]

var str = ' 123012301230456789 '
var result = Str.split (' a ');
Console.log (result);
["123012301230456789"]

var str = ' 123012301230456789 '
var result = Str.split (");
Console.log (result);
["1", "2", "3", "0", "1", "2", "3", "0", "1", "2",
"3", "0", "4", "5", "6", "7", "8", "9"]


charCodeAt () returns the Unicode encoding of the character at the specified position, and the result must be a number
var str = ' Hello ';
Console.log (Str.charcodeat (1)); 101

The concat () connection string, which converts all its arguments to a string,
It then connects sequentially to the end of the string stringobject and returns the concatenated string
Same array usage Note: + also has the same function
var str = ' Hello ';
var result = Str.concat (' js ', ' haha ')
Console.log (result);
Hellojshaha
Console.log (str);
Hello

IndexOf () returns the position of the first occurrence of a string fragment in a string, from left to right
var str = ' Hello ';
var result = Str.indexof (' el ');
Console.log (result); 1

var str = ' Hello '; Search from left to right, search for the first L's Corner label
var result = Str.indexof (' l ');
Console.log (result); 2

var str = ' Hello '; String not retrieved, return value is-1
var result = Str.indexof (' lll ');
Console.log (result); -1

var str = ' Hello, world ';
var result = Str.indexof (' l ', 6);
6 The representative starts with the first search
Console.log (result); 10

LastIndexOf () Search from right to left

Replace () replaces the character in a string, replacing only the first satisfied character,
If you want to replace all, you need to use regular expressions
var str = ' Hello, world ';
var result = str.replace (' l ', 0);
L: The character you want to replace 0: what to replace
Console.log (result);
He0lo,world
Console.log (str);
Hello,world


Slice () intercept fragment, same as array
var str = ' Hello, world ';
var result = Str.slice (3,-1);
Console.log (result); Lo, Worl

toUpperCase () Convert uppercase
var str = ' helLo, world ';
Console.log (str);
HelLo, World
var result = Str.touppercase ();
Console.log (result);
HELLO, World
Console.log (str);
HelLo, World

toLowerCase () Convert lowercase

The method of the Number object (Everything is objects, objects are used)
ToString () Any object has (JS bottom)
var a = ' 123 ';
var b = 100;
var result = a + B;
1. Convert A and B to the corresponding pseudo-object for reading
2, because the + symbol is a string connection operator, this time will call two pseudo-object ToString ()
method to stitch the return values of two methods together
Console.log (result); 123100

ValueOf () Converts a string to a number, which is owned by any object (JS bottom)
var a = ' 123 ';
var b = 100;
var result = A-B;
-only add and subtract operations
Console.log (result); 23

ToFixed () specifies that several decimals are kept, rounded, and the result is a string
var num = 1.23456;
Console.log (num.tofixed (2)); 1.23
Console.log ((1.2345). toFixed (2)); 1.23 Method Two


Toexexponential () Converts the result to an exponential count method with the result of a string
var num = 123456.789;
Console.log (Num.toexponential ()); 1.23456789e+5

Toprecision specify the number of digits of the exponential count method
var num = 123456.789;
Console.log (Num.toprecision (2)); 1.2e+5

Math Object
Property:
PI: Return pi
Console.log (Math.PI); 3.141592653589793

Method:
ABS (x) absolute value of the return number
Console.log (Math.Abs (10)); 10
Console.log (Math.Abs (-10)); 10

Min (x, y) max (x, y) is better in low-version ie than if
Console.log (Math.min (10,12)); 10
Console.log (Math.max (10,12)); 12

Ceil (x) rounding on a logarithmic
Console.log (Math.ceil (10.01)); 11
Console.log (Math.ceil (10.00)); 10

Floor (x) logarithm is rounded down
Console.log (Math.floor (10.99)); 10
Console.log (Math.floor (10.00)); 10
//
Round (x) rounding
Console.log (Math.Round (10.4)); 10
Console.log (Math.Round (10.6)); 11

Random (x) generates random decimals between 0-1
var num = Math.random ();
Console.log (num); 0.9598256822289126

Generate random numbers between 0-20
Randomly generate 0-1 decimals, multiply by 21, and then go down to rounding
Console.log (Math.floor (Math.random () *20));
Generate random numbers between 1-20
Randomly generate 0-1 decimals, multiply by 20, and then go up to the rounding

Date objects represent dates or times
var mydate = new Date (); Create a date way, no literal way
Console.log (mydate); Wed Oct 10:54:59 gmt+0800 (China Standard Time)

Method:
Date () Returns the day and time
Console.log (Date ()); Wed Oct 10:56:44 gmt+0800 (China Standard Time)


GetDate () returns the number of
var mydate = new Date ();
Console.log (Mydate.getdate ());
//
GetMonth () returns months 0 to month
var mydate = new Date ();
Console.log (Mydate.getmonth ());
//
getFullYear () return year
var mydate = new Date ();
Console.log (Mydate.getfullyear ());
//
GetDay () Returns the day of the week 0 for Sunday
var mydate = new Date ();
Console.log (Mydate.getday ());

GetHours () returns the number of hours getminutes/getseconds/getmilliseconds get milliseconds
Generate timestamps, get unique
(The low version browser can only be accurate to 10 milliseconds or 30 milliseconds)
var mydate = new Date ();
Console.log (Mydate.gethours ());

getTimezoneOffset () Get the time difference between local and standard time (minutes)
var mydate = new Date ();
Console.log (Mydate.gettimezoneoffset ());

getUTCHours () Get the hour of standard Time
var mydate = new Date ();
Console.log (Mydate.getutchours ());

Sethours () set several
var mydate = new Date ();
Mydate.sethours (10); Set the current time to 10 points
Console.log (Mydate.getutchours ());

ToString converts the current date to a string in the format English
var mydate = new Date ();
Console.log (Mydate.tostring ());

toLocaleString () show current date according to local custom
var mydate = new Date ();
Console.log (Mydate.tolocalestring ());

toTimeString () Get time only
var mydate = new Date ();
Console.log (Mydate.totimestring ());

toLocaleTimeString () according to local habits
var mydate = new Date ();
Console.log (Mydate.tolocaletimestring ());

Methods for js-objects

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.