2019-06-21

字符串

多行字符串

  • 如果长字符串必须分成多行,可以在每一行的尾部使用反斜杠。反斜杠的后面必须是换行符。
var str = 'long \
long \
long ';
  • 连接运算符(+)可以连接多个单行字符串,用来模拟多行字符串。
var str = 'long'
+ 'long'
+ 'long'
+ 'string'; 
  • ES6扩展
var str = `
hello
world
`

常见字符串方法

1、长度计算

var str = 'hello world';
console.log(str.length)//11
console.log(str[0])//h 第一个字符
console.log(str[str.length-1])//d 最后一个字符
console.log(str.charAt(0))//h 第一个字符
console.log(str.charCodeAt(0))//d 最后一个字符的编码

2、连接

3、字符串截取

console.log(str.substr(1,3))//ell 第一个是开始位置,第二个是长度
console.log(str.substring(1,3))//el 第一个是开始位置,第二个是结束位置
console.log(str.slice(1,3))//el 第一个是开始位置,第二个是结束位置 允许负参

4、查找

console.log(str.search('ll'))// 2  找不到为-1
console.log(str.replace('ll','hh'))//hehho world
console.log(str.match('ll'))//返回匹配的数组

5、大小写

console.log(str.toUpperCase())//HELLO WORLD
console.log(str.toLowerCase())//hello world

Math

Math.PI //3.1415926

round

四舍五入

Math.round(0.5)// 1
Math.round(-1.5)//-1
Math.round(-1.6)//-2

abs

参数绝对值

Math.abs(-1)//1

max min

Math.max(2,-1,5) //5
Math.min(2,-1,5)//-1

floor

参数值的最大整数

Math.floor(-3.6)//-4
Math.floor(3.6)//3

ceil

参数值的最小整数

Math.ceil(3.6)//4
Math.ceil(-3.6)//-3

pow

第一个参数为底数、第二个参数为幂的指数值

Math.pow(2,3)//8

sqrt

返回参数值的平方根。如果参数是一个负值,则返回NaN。

Math.sqrt(9,2)//3

log

以e为底的自然对数值

Math.log(Math.E)//1
Math.log(100)/Math.LN10//2

exp

exp方法返回常数e的参数次方

Math.exp(1)//2.718281828459045
Math.exp(2)//7.38905609893065

random

该方法返回0到1之间的一个伪随机数,可能等于0,但是一定小于1

Math.random()//0-1的随机数

给定范围内的随机数

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
getRandomArbitrary(5,11)

给定范围内的随机整数

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
getRandomInt(5,11)

三角函数

sin cos tan
asin acos atan

Date

Date.now()

当前距离1970年1月1日00:00:00的毫秒数

Date.now()//1561184303265

Date.parse()

parse方法用来解析日期字符串,返回距离1970年1月1日 00:00:00的毫秒数。日期字符串的格式应该完全或者部分符合YYYY-MM-DDTHH:mm:ss.sssZ格式,Z表示时区,是可选的。
如果解析失败,返回NaN

Date.parse(’2019-10-1‘)

new Date()

var d = new Date()

    d.getTime()         //返回实例对象距离1970年1月1日00:00:00对应的毫秒数
    d.getDate()         //返回实例对象对应每个月的几号(从1开始)
    d.getDay()          //返回星期,星期日为0,星期一为1,以此类推
    d.getFullYear()     //返回四位的年份
    d.getMonth()        //返回月份(0表示1月,11表示12月)
    d.getHours()        //返回小时(0~23)
    d.getMilliseconds() //返回毫秒(0-999)
    d.getMinutes()      //返回分钟(0-59)
    d.getSeconds()      //返回秒(0-59)

Date运算

var curTime = Date.now();
console.log(curTime-100*24*60*60*1000);
//距离100天之前有xxxx毫秒

你可能感兴趣的:(2019-06-21)