js内置对象和对象方法

内置对象(Math)

  • Math.PI 表示圆周率

console.log(Math.PI);  // 3.141592653589793

  •  Math.abs()可以用来计算一个数的绝对值

console.log(Math.abs(-3));  //  3

  •  Math.ceil() 可以用来对一个数向上取整,小数位只要有值,就自动进1

console.log(Math.ceil(1.00001));  // 2

  •  Math.floor() 可以对一个数进行向下取整,小数部分会被舍掉

console.log(Math.floor(-2.90001));  // -3

  •  Math.round() 可以对一个数进行四舍五入取整

console.log(Math.round(1.6));  // 2

  • Math.random() 可以生成一个0-1之间的随机数

                              -生成一个0-x之间的随机数Math.round(Math.random()*x)

                              -生成一个x-y之间的随机数Math.round(Math.random()*(y-x)+x

console.log(Math.random() * 10);   //  0-10之间的随机数

console.log(Math.round(Math.random() * n));  //  0-n之间的随机整数

Math.round(Math.random() * (y - x)) + x;  //  x-y之间的随机整数

  • Math.max() 可以获取多个数中的最大值

console.log(Math.max(111,4,7,23));  //  111

  • Math.min() 可以获取多个数中的最小值

console.log(Math.min(111,42,7,23));  //  7

  • Math.pow(x,y) 返回x的y次幂

console.log(Math.pow(2, 4));  //  16

  • Math.sqrt()  对一个数进行开方运算

console.log(Math.sqrt(9));  //  3

内置对象(Date)

时间对象

var date = new Date();    // 获得当前时间

var year = date.getFullYear();   //  获取年份

var month = date.getMonth() + 1;   // 获取月份

var myDate = date.getDate();   //  获取天数

var week = date.getDay();   // 获取星期数

var hours = date.getHours();  //  获取小时

var minutes = date.getMinutes();  // 获取分钟

var seconds = date.getSeconds();  // 获取秒数

字符串方法

1.charAt()  -获取指定字符

  • 可以返回字符串中指定位置的字符,根据索引获取指定的字符

var str = 'hello'

console.log(str.charAt(1));  // e

2.concat()  -拼接

  • 可以用来连接两个或者多个字符串

  • 作用和+一样

var str = 'hello'

var str1 = '你好'

var res = str.concat(str1)

console.log(res);  // hello 你好

3.indexof()  -判断有没有相同的内容

  • 该方法可以检索一个字符串中是否含有指定内容

  • 如果字符串中含有该内容,则会返回其第一次出现的索引

                 如果没有找到指定的内容,则返回-1

  • 可以指定第二个参数,指定开始查找的位置

var str = 'hello'

console.log(str.indexOf('w'));  // -1

console.log(str.indexOf('o'));  // 4

4.lastIndexOf()  -从最后开始找

  • 该方法的用法和indexOf()一样

  • 区别是lastIndexOf是从后往前找

var str = 'hello'

console.log(str.lastIndexOf('w'));  // -1

console.log(str.lastIndexOf('h'));  // 0

5.slice()  -截取

  • 可以从字符串中截取指定的内容

  • 不会影响原字符串,而是将截取到的内容返回

  • 参数:

                  第一个,开始位置的索引(包括开始位置)

                  第二个,结束位置的索引(不包括结束的位置)

  • 如果省略第二个参数,则会截取后边所有的

  • 也可以传递一个负数作为参数,负数的话将会从后边计算

var str = 'hello'

console.log(str.slice(1));  // ello

console.log(str.slice(1, 3));  // el

console.log(str.slice(2, -1));  // ll

6.substring()  -截取

  • 用来截取一个字符串,跟slice()类似

  • 参数:

                  第一个,开始位置的索引(包括开始位置)

                  第二个,结束位置的索引(不包括结束的位置)

  • 如果省略第二个参数,则会截取后边所有的

  • 跟slice()不同的是这个方法不能接收负值作为参数

                     如果传递了一个负值,则默认使用0

  • 还会自动调整参数的位置,如果第二个参数小于第一个

                   则自动交换

var str = 'hello'

console.log(str.substring(1));  // ello

console.log(str.substring(1, 3));  // el

console.log(str.substring(2, -1));  // he

7.substr()  -截取

  • 用来截取字符串

  • 参数:

                  1:截取开始位置的索引

                  2:截取的长度

var str = 'hello'

console.log(str.substr(1));  // ello

console.log(str.substr(1, 3));  // el

8.split()  -将字符串拆分成数组

  • 可以将一个字符串拆分为一个数组

  • 参数

                 需要一个字符串作为参数,将会根据该字符串去拆分数组

                 如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素

var str = "abc,bcd,efg,hij";

console.log(str.split(","));  // ['abc', 'bcd', 'efg', 'hij']

9.toUpperCase()  -字母转大写

  • 将一个字符串转换为大写并返回

var str = "abcdefg";

console.log(str.toLocaleUpperCase());  // ABCDEFG

10.toLowerCase()  -字母转小写

  • 将一个字符串转换为小写并返回

var str = "ABCDEFG";

console.log(str.toLocaleLowerCase());  // abcdefg

  • 27
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值