ES6基础(day02)

1、ECMAScript6中字符串的用法
1.1 模版字符串
这里的模板指的是上面讲的字符串模板,用反引号定义的字符串;而标签,则指的是一个函数,一个专门处理模板字符串的函数。

通俗理解: console.log(`哈哈哈哈哈`)
相对于原先拼接字符串的时候,避免了引号使用的错误。
1.2 repeat函数

var a = '哈'
console.log(a.repeat(5)) //重复五遍
1.3 includes函数

var b = 'asdfghjkl'
console.log(b.includes('g')) //包含返回true
console.log(b.includes('c')) // 不包含返回false
1.4 startsWith函数

console.log(b.startsWith('a')) //起始位置为a返回 true 反之false
console.log(b.startsWith('s',1)) //第二个参数为索引值 从索引为1开始 为s返回true
1.5 endsWith函数

console.log(b.endsWith('l')) //末尾位置为l返回 true 反之false
console.log(b.endsWith('k',8)) //第二个参数为前方查询内容的所在个数位置 第8个为k返回true
1.6 string.raw函数
未经String.raw( )处理的字符串:

 \n会被识别为换行符,实现换行效果,而经过String.raw( )的同一个字符串的结果是:

2、ECMAScript6中数值的用法
2.1传统写法

2.2 Number.isNaN函数

console.log(Number.isNaN('abc'))
// 总结:挂在到Number上之后,传入的值是数字或者字符串返回结果都为false。 Infinity 无穷
2.3 Number.isFinite函数

console.log(Number.isFinite(1));
console.log(Number.isFinite(Infinity));
console.log(Number.isFinite(Math.PI));
console.log(Number.isFinite('-∞'));
console.log(Number.isFinite('abc'));
// 总结: isFinete 验证数字的,只有infinity是无穷,其他的数字都是有穷的,如果是字符串,则返回false
2.4 Number.parseInt函数

console.log(Number.parseInt(3.5));
2.5 Number.isInteger函数

console.log(Number.isInteger(3));//true

console.log(Number.isInteger(3.4));//false

console.log(Number.isInteger(3.0));//true

console.log(Number.isInteger(3.02));//false

// 总结:isInteger 用来判断是否是整数,小数点后面均为0,则认为是整数

2.6 Math.trunc函数

console.log(Math.trunc(3.5))

// Math方法 增加取整

2.7 Math.sign函数

 

console.log(Math.sign(0));

 

console.log(Math.sign(-5));

console.log(Math.sign(5));

// Math.sign() 判断一个数是整数,负数,还是0

3、ECMAScript6中数组的用法

3.1 Array.of函数

// 检测数据类型方式 typeof instanceof constructor Object.prototype.toString.call()

 

// ES6 新增的Array.of() 可以把任意数据类型转换为数组

 

 var res = Array.of(1,2,3,4,5);

console.log(res);

3.2 Array.from函数

 

 

 

 

 

 

 

 

 

var res = Array.from(oLi).reverse();

console.log(res);

// 注意:Array.from() 可以放两个参数,第一个是要转变的对象,但是此对象必须要有长度,第二个参数是回掉函数,是指完成前面的转换后做什么事情

3.3 find函数

 

 

 

 

 

 // find() 查找满足条件的第一个参数

// var arr1 = [7,2,4,6]

// var r = arr1.find(function(val){

// return val>4

// })

// console.log(r)

 

 

 3.4 findIndex函数

 

 

 // findIndex() 查找满足条件的第一个参数的索引值

// var arr1 = [7,2,4,6]

// var r = arr1.findIndex(function(val){

// return val>4

// })

// console.log(r)

3.5 fill函数

 

 

 

 

 

 // fill() 把数组中的内容用什么填充

// var arr1 = [2,3,4,5];

// var ress = arr1.fill('a');

// console.log(ress);

// var arr1 = [2,3,4,5,7,3,2,7,8,9,0,19];

// var ress = arr1.fill('a',1,5);

// console.log(ress);

// 总结:fill(替换内容,开始索引,结束索引但是不包括该索引内容)

3.6 entries函数 3.7 keys函数 3.8 values函数

 

 

 

 

 

 // entries() keys() values()

var arr = [{a:1},2,3,4,5]

for(let val of arr.entries()){

console.log(val);

}

for(let val of ['a','b','c','d'].keys()){

console.log(val);

}

for(let val of ['a','b','c','d'].values()){

console.log(val);

}

4ECMAScript6中对象的使用

4.1对象的传统写法

 

 

 

 

 

 

 

 

 简写表示法:

 

 

 

 4.2ECMAScript6中属性名的更新

 

 

 

 

 4.3Object.is函数

// Object.is() 完成相等的比较

 

 

 

 

 

 4.4Object.assign函数

Object.assign(目标值,插入值) 类似与数组中的合并

 

 

 

 4.5Object.getPrototypeOf函数 4.6Object.setPrototypeOf函数

// 构造函数

function Person(){

}

Person.prototype = {

say(){

console.log('say hello');

},

work(){

console.log('teach');

}

}

var p = new Person();

// p.say();

console.log(Object.getPrototypeOf(p))

Object.setPrototypeOf(

p,

{

say(){console.log('abc')},

work(){console.log('123')}

}

)

p.say();

p.work();

4.7Javascript中面向对象

 

 

 

 5ECMAScript6中函数的使用

5.1参数的默认值

传统方式:

 

 

 

 

 

 传统写法:

// function fn(name,age){

// var name = name||'zs';

// var age = age||15;

// console.log(name,age);

// }

// fn('ls',18)

简易写法

function fn(age=13){

console.log(age);

}

fn(NaN)

// 默认值如果传递的是对象,则只有undefinedNaN和空可以触发默认值 其他情况传入值就是实际值

// 默认值如果传递的是变量,则只有undefined,空可以触发默认值 其他情况传入值就是实际值

5.2rest函数

 

 

 

 5.3扩展运算符

 

 

 

 一、// 扩展运算符

// function total(a,b){

// console.log(a,b)

// return a+b;

// }

// let arr = [1,2]

// console.log(total(...arr))

二、

// 扩展运算符 (...

// // 求和

// function sun(result,...values){

// console.log(values); //这是一个数组

// values.forEach(function(v,i){

// // console.log(v,i)

// result+=v;

// })

// console.log(result);

// }

// var res = 0;

// sun(res,1,2,3,4,5)

5.4箭头函数

 

 

 

 箭头函数

var sun = function(a){

console.log(a);

}

var sum = a=>{console.log(a)};

sum(1);

 

 

 

 

posted on 2020-08-06 22:15  Mark++  阅读(71)  评论(0)    收藏  举报

导航