es6基础(7)--函数扩展】的更多相关文章

{ //有默认值的后面如果有参数必须要有默认值 function test(x,y="world"){ console.log(x,y) } test('hello');//hello world test('hello',"kill");//hello kill } { let x='test'; function test2(x,y=x){ console.log('作用域',x,y); } test2('kill');//kill kill function…
函数默认参数 function test(x = 1, y = 2) { return x + y } test(5, 6) test() 若默认参数在必须参数之前,要想取得默认参数,只有当传入的值为undefined才能取到 function test(x = 1, y) { console.log(x,y) } test(5, 6) //5,6 test(1) //3 undefined test(null,1) //null 1 test(undefined,1) //1,1 参数默认值是…
//函数参数默认值(more值后不能有参数) { function test(x,y = 'world'){ console.log('默认值',x,y); } test('hello');// hello world test('hello','kill'); //hello kill } //作用域概念 { let x = 'test'; function test2(x,y = x){ console.log('作用域',x,y); } test2(); // undefined unde…
//正则扩展 { let regex=new RegExp('xyz','i'); let regex2=new RegExp(/xyz/i); console.log(regex.test('xyz123'),regex2.test('xy')); //后面的修饰符i覆盖原来的ig修饰符 let regex3=new RegExp(/xyz/ig,'i'); console.log(regex3.flags); } { let s='bbb_bb_b'; //g,y都是全局匹配 let a1=…
4.字符串扩展 (1)for...of循环遍历. let foo = [1,2,3,4,5,6] for(let i of foo){ console.log(i); } 结果: (2)includes().startsWith().endsWith() JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中[返回某个指定的字符串值在字符串中首次出现的位置]. ES6 又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. sta…
//数组扩展 { let arr=Array.of(3,4,6,7,9,11);//可以是空 console.log('arr=',arr);//[3,4,6,7,9,11] } { //Array.from把伪数组或者集合变为数组 let p=document.querySelectorAll('p'); let pArr=Array.from(p); pArr.forEach(function(item){ console.log(item.textContent); }) //类似map…
//字符串扩展 { console.log('a','\u0061'); console.log('s','\u20BB7');//超过了0xffff console.log('s','\u{20BB7}');//如果超过就用{}包裹 } { //es5中 let s='…
{ //Number.isFinite数字是有尽的 console.log(Number.isFinite(15));//true console.log(Number.isFinite(NaN));//false console.log(Number.isFinite('true'/0));//false console.log(Number.isNaN(NaN));//true console.log(Number.isNaN(10));//false } { //判断是否为整数,括号里面必…
× 目录 [1]参数默认值 [2]rest参数 [3]扩展运算符[4]箭头函数 前面的话 ES6标准关于函数扩展部分,主要涉及以下四个方面:参数默认值.rest参数.扩展运算符和箭头函数 参数默认值 一般地,为参数设置默认值需进行如下设置 function log(x, y) { y = y || 'World'; console.log(x, y); } 但这样设置实际上是有问题的,如果y的值本身是假值(包括false.undefined.null.''.0.-0.NaN),则无法取得本身值…
前面的话 函数是所有编程语言的重要组成部分,在ES6出现前,JS的函数语法一直没有太大的变化,从而遗留了很多问题和的做法,导致实现一些基本的功能经常要编写很多代码.ES6大力度地更新了函数特性,在ES5的基础上进行了许多改进,使用JS编程可以更少出错,同时也更加灵活.本文将详细介绍ES6函数扩展 形参默认值 Javascript函数有一个特别的地方,无论在函数定义中声明了多少形参,都可以传入任意数量的参数,也可以在定义函数时添加针对参数数量的处理逻辑,当已定义的形参无对应的传入参数时为其指定一个…