前端知识体系(JS篇)之常用方法

66 阅读6分钟

src=http___img.tt98.com_d_file_pic_2018112316244430_001.jpg&refer=http___img.tt98.webp

Object数组:

  1. toString() 方法把数组转换为数组值(逗号分隔)的字符串
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theSecoend = theFist.toString()
console.log(theSecoend) //结果:一拳,二拳,三拳,认真一拳
  1. pop() 从数组中删除最后一个元素,结果也返回删除的元素 会破坏原数组!
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theSecoend = theFist.pop()
console.log(theSecoend) //结果:认真一拳
  1. push() 向数组尾部添加一个新的元素 会破坏原数组!
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theFist1 = theFist.push("掉头发了")
console.log(theFist) //结果:[ '一拳', '二拳', '三拳', '认真一拳', '掉头发了' ]
  1. shift() 会删除数组首个元素 会破坏原数组!
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theFist1 = theFist.shift()
console.log(theFist) //结果:[ '二拳', '三拳', '认真一拳' ]
  1. unshift() 向数组开头添加元素 会破坏原数组!
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theFist1 = theFist.shift("零拳")
console.log(theFist) //结果:[ '零拳', '一拳', '二拳', '三拳', '认真一拳' ]
  1. splice() 向数组(添加/删除元素) 会破坏原数组!

一:参数说明:

1.第一个参数(1)定义为应添加新元素的位置(拼接)
2.第二个参数(0)定义应删除多少个元素。
3.其余参数("掉头发")定义要添加的新元素。

//向数组添加元素
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theFist1 = theFist.splice(1, 0, "掉头发")
console.log(theFist) //结果:[ '一拳', '掉头发', '二拳', '三拳', '认真一拳' ]

二:参数说明:

1.第一个参数(2)定义为应添加新元素的位置(拼接)
2.第二个参数(2)定义应删除多少个元素。
3.其余参数("掉头发")定义要添加的新元素。

//向数组删除元素
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theFist1 = theFist.splice(2, 2, "掉头发")
console.log(theFist) //结果:[ '一拳', '二拳', '掉头发' ]
  1. sort() 以字母顺序对数组进行排序 会破坏原数组!
const theFist = ["B", "A", "D", "C"]
const theFist1 = theFist.sort()
console.log(theFist1) //结果:[ 'A', 'B', 'C', 'D' ]
  1. reverse() 以字母顺序对数组进行排序 会破坏原数组!
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theFist1 = theFist.reverse()
console.log(theFist) //结果:[ '认真一拳', '三拳', '二拳', '一拳' ]
  1. Math.max()返回一组数中的最大值。
const theFist = [1, 2, 3]
const theFist1 = Math.max(...theFist)
console.log(theFist1) //结果:3
  1. Object.assign()合并两个或多个object。 这也是浅拷贝其中方法之一哟!
const thefirst = {a: 1, b: 2}
const thefirst1 = {c: 3, d: 4}
const obj = Object.assign(thefirst, thefirst1)
console.log(obj) //返回 { a: 1, b: 2, c: 3, d: 4 }
  1. concat() 通过合并(连接)现有数组来创建一个新数组 不破坏原数组!
const theFist2 = ["波赛西", "波塞冬"]
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theFist1 = theFist.concat(theFist2)
console.log(theFist1) //结果:[ '一拳', '二拳', '三拳', '认真一拳', '波赛西', '波塞冬' ]
  1. slice() 可以切割出新数组 不破坏原数组!
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theSecoend = theFist.slice(1, 2)
console.log(theSecoend) //结果:[ '二拳' ]

一:参数说明:

1.第一个参数(1)定义为从什么位置选取元素开始切割
2.第二个参数(2)为切割结束位置但不包括(注:选取前一项)

  1. join() 传入自定义内容,将所有数组元素用自定义内容拼接为一个字符串 不破坏原数组!
const theFist = ["一拳", "二拳", "三拳", "认真一拳"]
const theSecoend = theFist.join(" - ")
console.log(theSecoend) //结果:一拳 - 二拳 - 三拳 - 认真一拳
  1. map()返回新数组,数组中元素是原数组元素调用函数方法处理后的值 不破坏原数组!
const thefirst = [1, 2]
const res = thefirst.map(value => value + 1)
console.log(res) //返回 [ 2, 3 ]

String字符串:

  1. indexOf() 返回字符串中指定文本首次出现的索引(位置)
const theFist = "一拳二拳三拳认真一拳"
const theFist1 = theFist.indexOf("二拳")
console.log(theFist1) //结果:2
  1. lastIndexOf() 返回指定文本在字符串中最后一次出现的索引(位置)
const theFist = "一拳二拳三拳认真一拳二拳"
const theFist1 = theFist.lastIndexOf("二拳")
console.log(theFist1) //结果:10
  1. includes() 用于判断字符串是否包含指定的子字符串。 //如果找到匹配的字符串则返回 true,否则返回 false
const theFist = "一拳二拳三拳认真一拳二拳"
const theSecond = theFist.includes("二拳")
console.log(theSecond) //结果:true
  1. replace() 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串 不破坏原字符串!
const theFist = "一拳二拳三拳认真一拳二拳"
const theSecond = theFist.replace("二拳","掉头发")
console.log(theSecond) //结果:一拳掉头发三拳认真一拳二拳
  1. split() 把字符串分割为字符串数组
const theFist = "一拳二拳三拳认真一拳二拳"
const theSecond = theFist.split("")
console.log(theSecond) //结果:['一', '拳', '二', '拳', '三', '拳', '认', '真', '一','拳', '二', '拳']
  1. substr() 从起始索引号提取字符串中指定数目的字符
const theFist = "一拳二拳三拳认真一拳二拳"
const theSecond = theFist.substr(2,3)
console.log(theSecond) //结果:二拳三
  1. substring() 提取字符串中两个指定的索引号之间的字符
const theFist = "一拳二拳三拳认真一拳二拳"
const theSecond = theFist.substring(2, 4)
console.log(theSecond) //结果:二拳
  1. trim() 提取字符串中两个指定的索引号之间的字符
const theFist = "     一拳二拳三拳认真一拳二拳"
const theSecond = theFist.trim()
console.log(theSecond) //结果:一拳二拳三拳认真一拳二拳
  1. toString() 返回一个字符串
const theFist = "一拳二拳三拳认真一拳二拳"
const theSecond = theFist.toString()
console.log(theSecond) //结果:一拳二拳三拳认真一拳二拳

Number对象:

  1. valueOf() 返回一个 Number 对象的基本数字值
const theFist = "一拳二拳三拳认真一拳二拳"
const theSecond = theFist.valueOf()
console.log(theSecond) //结果:一拳二拳三拳认真一拳二拳
  1. toFixed() 把数字转换为字符串,结果的小数点后有指定位数的数字
const theFist = "一拳二拳三拳认真一拳二拳"
const theSecond = theFist.toString()
console.log(theSecond) //结果:一拳二拳三拳认真一拳二拳

Math对象:

  1. random() 返回 0 ~ 1 之间的随机数
console.log(Math.random()) //0.3288344483214145
  1. round() 四舍五入
console.log(Math.random()) //0.3288344483214145
  1. floor(x) 对x 进行下舍入
console.log(Math.floor(1.6)) //结果 1
  1. ceil(x) 对数进行上舍入
console.log(Math.ceil(2.6)) //结果 3

Date对象:

  1. getDate() 从 Date 对象返回一个月中的某一天
console.log(Math.random()) //0.3288344483214145
  1. 总结Date对象的用法
    dt.getFullYear();//年

    dt.getMonth();//月---从0开始

    dt.getDate();//日

    dt.getHours();//小时

    dt.getMinutes();//分钟

    dt.getSeconds();//秒

    dt.getDay();//星期---从0开始

    dt.toDateString();//日期

    dt.toLocaleDateString();//日期

    dt.toTimeString();//时间

    dt.toLocaleTimeString();//时间
   
    dt.valueOf();//毫秒

基本包装类型:

    对象可以直接调用属性和方法,普通变量不能直接调用属性或者方法

说明:

一:基本包装类型有Number、Boolean、String三种。主要扩展基本数据类型Number、Boolean、String的功能
二:5 种基本数据类型:Undefined、Null、Boolean、Number 和 String

Math:

  1. getDate() 从 Date 对象返回一个月中的某一天
    Math.PI----π---

    Math.E----常数的底数

    Math.abs(值)-----绝对值

    Math.ceil(值)----向上取整

    Math.floor(值)---向下取整

更多内容持续跟新(包括本章内容)......记得关注点个赞!