ES6 String和Number扩展
一、String 扩展
①传统上,JavaScript 只有indexOf
方法,可以用来确定一个字符串是否包含在另一个字符串中。ES6 又提供了三种新方法。
- includes():返回布尔值,表示是否找到了参数字符串。第二个参数表示搜索起始位置
- startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。第二个参数表示搜索起始位置
- endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。第二个参数针对前
n
个字符,而其他两个方法针对从第n
个位置直到字符串结束
let s = 'Hello world!'; s.startsWith('Hello') // true
s.endsWith('!') // true
s.includes('o') // true
let s = 'Hello world!'; s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true
s.includes('Hello', 6) // false
②repeat
方法返回一个新字符串,表示将原字符串重复n
次。参数如果是小数,会被取整。如果repeat
的参数是负数或者Infinity
,会报错。
'x'.repeat(3) // "xxx"
'hello'.repeat(2) // "hellohello"
'na'.repeat(0) // "" 'na'.repeat(2.9) // "nana" 'na'.repeat(Infinity)
// RangeError
'na'.repeat(-1)
// RangeError 'na'.repeat(-0.9) // "" na'.repeat(NaN) // "" 'na'.repeat('na') // ""
'na'.repeat('3') // "nanana"
③模板字符串
传统的 JavaScript 语言,输出模板通常是这样写的。
$('#result').append(
'There are <b>' + basket.count + '</b> ' +
'items in your basket, ' +
'<em>' + basket.onSale +
'</em> are on sale!'
);
上面这种写法相当繁琐不方便,ES6 引入了模板字符串解决这个问题。
$('#result').append(`
There are <b>${basket.count}</b> items
in your basket, <em>${basket.onSale}</em>
are on sale!
`);
模板字符串(template string)是增强版的字符串,用反引号(`)标识。它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量。模板字符串中嵌入变量,需要将变量名写在${}
之中。
function authorize(user, action) {
if (!user.hasPrivilege(action)) {
throw new Error(
// 传统写法为
// 'User '
// + user.name
// + ' is not authorized to do '
// + action
// + '.'
`User ${user.name} is not authorized to do ${action}.`);
}
}
二、Number 扩展
①ES6 将全局方法parseInt()
和parseFloat()
,移植到Number
对象上面,行为完全保持不变。样做的目的,是逐步减少全局性方法,使得语言逐步模块化。
// ES5的写法
parseInt('12.34') //
parseFloat('123.45#') // 123.45 // ES6的写法
Number.parseInt('12.34') //
Number.parseFloat('123.45#') // 123.45
②Number.isInteger()
用来判断一个数值是否为整数,JavaScript 内部,整数和浮点数采用的是同样的储存方法,比如 25 和 25.0 被视为同一个值。如果参数不是数值,Number.isInteger
返回false
。
Number.isInteger(25) // true
Number.isInteger(25.1) // false
Number.isInteger(25.0) // true
Number.isInteger() // false
Number.isInteger(null) // false
Number.isInteger('15') // false
Number.isInteger(true) // false
③ES6 在 Math 对象上新增了 17 个与数学相关的方法。所有这些方法都是静态方法,只能在 Math 对象上调用。
Math.trunc
方法用于去除一个数的小数部分,返回整数部分。Math.sign
方法用来判断一个数到底是正数、负数、还是零。对于非数值,会先将其转换为数值。Math.cbrt
方法用于计算一个数的立方根。
更多详细知识点,请参考ECMAScript 6 入门。
ES6 String和Number扩展的更多相关文章
- ES6(四) --- 正则 Number Math
想学vue了 重启ES6的学习之路 在ES5 中正则的构造器 RegExp 不支持第二个参数 ES6 做了调整 第二个参数表示正则表达式的修饰符(flag) var regex = new ...
- ES6—数值(Number,Math对象)(复习+学习)
ES6-数值(Number,Math对象)(复习+学习) 每天一学,今天要学习ES6的关于数的扩展以及复习,然后通过看书,查阅资料,以及webAPI来搞清楚遇到的,没见过的对象方法等等,下面为本次学习 ...
- 五分钟了解ES6对数值的扩展
文章目录 数值的扩展(ES6) 1. 二进制八进制表示法 2. Number对象 3. Math对象 4. 指数运算符 5. Integer 数据类型 5.1 简介 5.2 运算 数值的扩展(ES6) ...
- JavaScript在IE6,IE7下报错'expected identifier, string or number'
问题: 代码在Forefox和IE8下工作正常,但是在IE6下报错: expected identifier, string or number 假如变量options有多个选项,那么我们可以用逗号分 ...
- ASP.Net string 类的扩展方法 [转]
string 类的扩展方法列表(基本相同于 IEnumerable<T> 接口的成员列表): Aggregate<> //累加 All<> / ...
- IE6,IE7,IE8下报JS错误:expected identifier, string or number的原因及解决的方法
今天在调试一个页面的时候遇到一个问题,在IE9下执行得非常好的脚本,在IE8里打开的时候弹出错误:expected identifier, string or number,依照经验,应该是定义对象的 ...
- ECMAScript6之String类型的扩展
String类型的扩展 模板字符串 模板字符串是字符串的增强版,既可以当做普通的字符串使用,也可以在字符串中嵌入变量,它用反引号`来表示. //普通字符串 `In javascript '\n' is ...
- ie6 ie7下报脚本错误"Expected identifier, string or number" 的原因和解决方法
在IE6和ie7里面,脚本报错"Expected identifier, string or number" 写下这个是个之前我已经很头疼了,因为我的代码在其他浏览器里都是正常的, ...
- ES6对数组的扩展(简要总结)
文章目录 数组的扩展(ES6) 1. 扩展运算符 2. Array.from 3. Array.of() 4. copyWithin() 5. find() 和 findIndex() 6. fill ...
随机推荐
- 使用Docker安装mysql,挂载外部配置和数据
.挂载外部配置和数据安装 mkdir /opt mkdir /opt/mysql mkdir /opt/mysql/conf.d mkdir /opt/mysql/data/ 创建my.cnf配置文件 ...
- 【spring boot】【redis】spring boot基于redis的LUA脚本 实现分布式锁
spring boot基于redis的LUA脚本 实现分布式锁[都是基于redis单点下] 一.spring boot 1.5.X 基于redis 的 lua脚本实现分布式锁 1.pom.xml &l ...
- Entity Framework 学习系列(2) - MySql Database First 开发方式
目录 写在前面 环境 下载MySQL连接工具 创建Databse First 1.创建控制台 2.创建数据库 3.安装 MySQL.Data 和MySQL.Data.Entity 3.在项目中添加数据 ...
- String类的方法应用
String类的几个方法的应用示例: using System;using System.Collections.Generic;using System.Linq;using System.Text ...
- .net core Identity注册用户 出错
使用微软自带的注册 报 NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named 'Default' is re ...
- selenium中元素操作之上传操作(六)
上传操作分为两种情况: 1.input标签上传 如果是input可以直接输入路径的,那么直接调用send_keys输入路径,和前边的元素操作类似,在这里不再过多的赘述. 2.非input标签上传 非i ...
- windows中Crontab的使用
一.jdk的安装 安装地址ttps://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 二 . ...
- codeforces#566(Div.2)B
B.Plus from Picture You have a given picture with size w×hw×h. Determine if the given picture has a ...
- PHPSocket.IO知识学习整理
一.服务端和客户端连接 1.创建一个SocketIO服务端 <?php require_once __DIR__ . '/vendor/autoload.php'; use Workerman\ ...
- SpringBoot+Security+MyBatis+ES+MQ+Redis+Docker+Vue的电商系统
今天鹏哥给大家推荐的项目是一套电商系统,包括前台商城系统及后台管理系统,基于SpringBoot+MyBatis实现. 前台商城系统包含首页门户.商品推荐.商品搜索.商品展示.购物车.订单流程.会员中 ...