博客
关于我
【必备知识点】JavaScript里面那些天天使用的API
阅读量:401 次
发布时间:2019-03-05

本文共 1379 字,大约阅读时间需要 4 分钟。

初学JavaScript的核心方法总结

数组操作

元素插入与删除

  • array.push(item...):向数组尾部添加元素。
  • array.unshift(item...):向数组头部添加元素。
  • array.concat(item...):将元素插入数组,可接受多个数组并在尾部拼接。

核心方法

  • array.pop():从数组尾部删除一个元素。
  • array.shift():从数组头部删除一个元素。
  • array.reverse():反转数组顺序。
  • array.slice(start, end):复制数组一部分。
  • array.join(separator):将数组元素转换为字符串。
  • array.sort(comparefn):对数组进行排序,可自定义比较函数。
  • array.splice(start, deleteCount, item...):实现高级数组操作,支持插入和删除。

数字处理

转换方法

  • number.toString():将数字转换为字符串。
  • Number.parseInt(item):将字符串或其他可转换为数字的值转换为整数。
  • Number.parseFloat(item):将字符串或其他可转换为浮点数的值转换为浮点数。

判断方法

  • Number.isNaN(item):判断是否为不定值(NaN)。
  • Number.isInteger(item):判断是否为整数。

对象操作

属性操作

  • object.hasOwnProperty(item):判断对象是否拥有指定属性。

获取方法

  • object.keys():返回对象所有属性名的数组。
  • object.values():返回对象所有属性值的数组。

JSON数据处理

序列化与反序列化

  • JSON.stringify():将对象转换为标准JSON字符串。
  • JSON.parse():将JSON字符串转换为对象。

字符串操作

常用方法

  • string.charAt(pos):获取指定位置的字符。
  • string.concat(...items):将字符串拼接成一个新的字符串。
  • string.indexOf(searchString, position):查找子字符串在字符串中的起始位置。
  • string.lastIndexOf(searchString, position):查找子字符串在字符串中的末尾起始位置。
  • string.search(regexp):使用正则表达式查找子字符串。
  • string.substring(start, end):截取字符串(支持负数参数)。
  • string.slice(start, end):高级截取方法,支持负数参数。
  • string.split(separator, limit):将字符串拆分成数组。
  • string.toLowerCase():将字符串转换为小写。
  • string.toUpperCase():将字符串转换为大写。

日期处理

常用方法

  • date.toLocaleString():按本地格式显示完整日期和时间。
  • date.toLocaleDateString():显示完整的日期部分。
  • date.toLocaleTimeString():显示完整的时间部分。

以上方法是前端开发中经常使用的核心工具,熟练掌握这些方法将极大提升您的开发效率。

转载地址:http://qmzwz.baihongyu.com/

你可能感兴趣的文章
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
查看>>