从零开始学 Web 之 ES6(六)ES6基础语法四
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新......
- github:https://github.com/Daotin/Web
- 微信公众号:Web前端之巅
- 博客园:http://www.cnblogs.com/lvonve/
- CSDN:https://blog.csdn.net/lvonve/
在这里我会从 Web 前端零基础开始,一步步学习 Web 相关的知识点,期间也会分享一些好玩的项目。现在就让我们一起进入 Web 前端学习的冒险之旅吧!

一、字符串的扩展
includes(str): 判断是否包含指定的字符串startsWith(str): 判断是否以指定字符串开头endsWith(str): 判断是否以指定字符串结尾repeat(count): 重复指定次数
let str = 'abcdefg';
console.log(str.includes('a')); //true
console.log(str.includes('h')); //false
//startsWith(str) : 判断是否以指定字符串开头
console.log(str.startsWith('ab')); //true
console.log(str.startsWith('ac')); //false
//endsWith(str) : 判断是否以指定字符串结尾
console.log(str.endsWith('fg')); //true
console.log(str.endsWith('d')); //false
//repeat(count) : 重复指定次数a
console.log(str.repeat(3)); // abcdefgabcdefgabcdefg
二、数值的扩展
二进制与八进制数值表示法: 二进制用0b开头, 八进制用0o开头。
Number.isFinite(i): 判断是否是有限大的数Number.isNaN(i): 判断是否是NaNNumber.isInteger(i): 判断是否是整数Number.parseInt(str): 将字符串转换为对应的数值Math.trunc(i): 直接去除小数部分
console.log(0b1010); //10
console.log(0o56); //46
console.log('--------------------');
//Number.isFinite(i) : 判断是否是有限大的数
console.log(Number.isFinite(NaN)); //false
console.log(Number.isFinite(5)); //true
console.log(Number.isFinite(Infinity)); //false
console.log('--------------------');
//Number.isNaN(i) : 判断是否是NaN
console.log(Number.isNaN(NaN)); //true
console.log(Number.isNaN(5)); //false
console.log(Number.isNaN(undefined)); //false
console.log('--------------------');
//Number.isInteger(i) : 判断是否是整数
console.log(Number.isInteger(5.23)); //false
console.log(Number.isInteger(5.0)); //true
console.log(Number.isInteger(5)); //true
console.log('--------------------');
//Number.parseInt(str) : 将字符串转换为对应的数值
console.log(Number.parseInt('123abc')); //123
console.log(Number.parseInt('a123abc')); //NaN
console.log('--------------------');
// Math.trunc(i) : 直接去除小数部分
console.log(Math.trunc(13.123)); //13
三、数组扩展
Array.from(v): 将伪数组对象或可遍历对象转换为真数组Array.of(v1, v2, v3): 将一系列值转换成数组find(function(value, index, arr){return true}): 找出第一个满足条件返回true的元素findIndex(function(value, index, arr){return true}): 找出第一个满足条件返回true的元素下标
<body>
<button>测试1</button>
<br>
<button>测试2</button>
<br>
<button>测试3</button>
<br>
<!--
1. Array.from(v) : 将伪数组对象或可遍历对象转换为真数组
2. Array.of(v1, v2, v3) : 将一系列值转换成数组
3. find(function(value, index, arr){return true}) : 找出第一个满足条件返回true的元素
4. findIndex(function(value, index, arr){return true}) : 找出第一个满足条件返回true的元素下标
-->
<script type="text/javascript">
//Array.from(v) : 将伪数组对象或可遍历对象转换为真数组,返回值即为真数组
// 使用 DOM操纵获取的元素集合是伪数组。
let btns = document.getElementsByTagName('button');
console.log(btns.length); //3
Array.from(btns).forEach(function (item, index) {
console.log(item, index);
});
//Array.of(v1, v2, v3) : 将一系列单独的值转换成数组
let arr = Array.of(1, 'abc', true);
console.log(arr);
//find(function(value, index, arr){return true}) : 找出第一个满足条件返回true的元素
let arr1 = [1, 3, 5, 2, 6, 7, 3];
let result = arr1.find(function (item, index) {
// 查找arr1数组中第一个大于3的元素
return item > 3;
});
console.log(result); //5
//findIndex(function(value, index, arr){return true}) : 找出第一个满足条件返回true的元素下标
let result1 = arr1.findIndex(function (item, index) {
// 查找arr1数组中第一个大于3的元素下标值
return item > 3;
});
console.log(result1); //2
</script>
</body>
四、对象扩展
Object.is(v1, v2):判断2个数据是否完全相等。(内部是的实现原理是比较字符串是否完全相等)
console.log(Object.is('abc', 'abc'));//true
console.log(NaN == NaN);//false
console.log(Object.is(NaN, NaN));//true
console.log(0 == -0);//true
console.log(Object.is(0, -0));//false
Object.assign(target, source1, source2..): 将源对象的属性复制到目标对象上
let obj = {name : 'Daotin', age : 18, c: {d: 2}};
let obj1 = {};
Object.assign(obj1, obj);
console.log(obj1, obj1.name);
- 直接操作
__proto__属性
let obj3 = {name : 'Daotin', age : 18};
let obj4 = {};
// obj4的隐式原型指向obj3
obj4.__proto__ = obj3;
console.log(obj4, obj4.name, obj4.age);// {name : 'Daotin', age : 18} Daotin 18
五、set容器和map容器
Set容器 : 无序不可重复的多个value的集合体。
Set容器需要通过new 来创建一个Set容器对象,参数即为多个value值。
let set = new Set([1,2,3,4,3,2,1,6]);
set容器对象的方法和属性:
Set():set容器的构造函数(不带参数value集合)Set(array):set容器的构造函数(带参数value集合)add(value):向set容器对象添加值valuedelete(value):删除set容器对象的value值has(value):判断set容器是否有value值clear():清空set容器size:相当于数组的length
Map容器 : 无序的 key不重复的多个key-value的集合体。
注意:Map的参数数组的集合,每一个数组都是key-value的形式。整个数组集合的外面用
[]包围,而不是{}。
let map = new Map([['name', 'Daorin'],['age', 18]]);
Map()
Map(array)
set(key, value) // 类似于set容器的add方法
get(key)
delete(key)
has(key)
clear()
size
Set和Map的作用:
1、Set容器可以为数组去重。
let arr = [1, 2, 3, 4, 5, 2, 4, 5];
let set = new Set(arr);
arr = [];
for (let i of set) {
arr.push(i);
}
console.log(arr); // 1,2,3,4,5
2、Set和Map容器可以使用for ..of.. 来遍历。
六、ES7 方法介绍
1、指数运算符:**
console.log(3**2); // 9
2、Array.prototype.includes(value); :判断数组中是否包含指定value。(在ES6中有,str.includes(str1) : 判断str中是否包含指定的字符串str1)
let arr = [1,2,3,4, 'abc'];
console.log(arr.includes(2));//true
console.log(arr.includes('a'));//false

从零开始学 Web 之 ES6(六)ES6基础语法四的更多相关文章
- 从零开始学 Web 之 ES6(四)ES6基础语法二
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 从零开始学 Web 之 ES6(五)ES6基础语法三
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 从零开始学 Web 之 ES6(三)ES6基础语法一
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 从零开始学 Web 之 ES6(一)ES5严格模式
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 从零开始学 Web 之 ES6(二)ES5的一些扩展
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 从零开始学 Web 之 Vue.js(六)Vue的组件
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 从零开始学 Web 之 DOM(六)为元素绑定与解绑事件
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... +-------------------------------------------------------- ...
- 从零开始学 Web 之 jQuery(六)为元素绑定多个相同事件,解绑事件
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 从零开始学 Web 之 Ajax(二)PHP基础语法
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
随机推荐
- 面试简单整理之JVM
194.说一下 jvm 的主要组成部分?及其作用? JVM内存分为“堆”.“栈”和“方法区”三个区域,分别用于存储不同的数据. 堆内存用于存储使用new关键字所创建的对象: 栈内存用于存储程序运行时在 ...
- NC nc开发工具java虚拟机参数
-Dnc.exclude.modules=${FIELD_EX_MODULES} -Dnc.runMode=develop -Dnc.server.location=${FIELD_NC_HO ...
- DVR NVR
1.NVR: 是(Network Video Recorder即网络硬盘录像机)的缩写.NVR最主要的功能是通过网络接收IPC(网络摄像机)设备传输的数字视频码流,并进行存储.管理,从而实现网络化带来 ...
- Java 日志体系
Java 日志体系 <java 日志和 SLF4J 随想>:http://ifeve.com/java-slf4j-think/ 一.常用的日志组件 名称 jar 描述 log4j log ...
- boost asio 网络聊天 代码修改学习
简化asio的聊天代码 去除ROOM的设计 所有连接客户端均在同一个ROOM下 /*********************************************************** ...
- java生成pdf文件 --- Table
Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...
- 10. Halloween 万圣节
10. Halloween 万圣节 (1) On October the 31st,across Britain and the USA,thousands of children are dress ...
- 使用freemarker对模板进行渲染
最近项目中使用到了,对word模板进行编辑和渲染,所以使用到了模板引擎技术. 在项目中,我们前端使用的富文本编辑器,进行展示和保存(和word格式一致),后端采用了freemarker进行数据的渲染. ...
- Django同步数据库(/manage.py makemigrations) 报错
新起了环境,创建models.py 内容,想要同步到数据库,执行以下操作时 报错: ./manage.py makemigrations ./manage.py migrate *(第一个步骤为在该项 ...
- java 支持 超大上G , 多附件上传
首先 确定要上传的目录 WEB.XML 文件 Java代码 <listener> <listener-class><!-- 临时文件收集器 , 支持超大附件必须项 - ...