ES6学习之函数扩展】的更多相关文章

函数默认参数 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 参数默认值是…
简介表示法(直接写入变量和函数,作为对象的属性和方法) let x = "test" let obj={ x, //属性名为变量名,属性值为变量值 y(){console.log("hello")} } //以上相当于 let obj = { x:'test', y:function(){ console.log("hello") } } 属性名表达式(表达式作为对象的属性名,把表达式放在方括号内.) let aa = "hello&q…
扩展运算符(...将数组分割为用逗号分割的参数序列) console.log(...[1,2,3]) //1 2 3 可替换数组的apply写法: function test(x,y,z){ console.log(x,y,z) } var arg = [1,2,3]; test.apply(null,arg) ////1 2 3 test(...arg) //1 2 3 扩展运算符的应用 复制数组 const a = [1, 2]; //复制数组a const b = [...a] //方法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…
1. 箭头函数基本形式 let func = (num) => num; let func = () => num; let sum = (num1,num2) => num1 + num2; [1,2,3].map(x => x * x); 2. 箭头函数基本特点 (1). 箭头函数this为父作用域的this,不是调用时的this 箭头函数的this永远指向其父作用域,任何方法都改变不了,包括call,apply,bind.普通函数的this指向调用它的那个对象. let pe…
1.函数默认参数 在ES5我们给函数定义参数默认值是怎么样? function action(num) { num = num || 200 //当传入num时,num为传入的值 //当没传入参数时,num即有了默认值200 return num } action(0) action("") // 200 action(1) // 1 但当,num传入为0的时候就是false, 此时num = 200 与我们的实际要的效果明显不一样 ES6为参数提供了默认值.在定义函数时便初始化了这个…
{ //有默认值的后面如果有参数必须要有默认值 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…
二进制和八进制表示法(二进制用前缀0b(或0B)表示,八进制用前缀0o(或0O)表示) Number('0b111') Number('0o10') Number.isFinite()(判断一个值是否有限) Number.isFinite(5) //true Number.isFinite("a") //false Number.isFinite(Infinity) //false Number.isFinite(true) //false Number.isNaN()(判断一个值是否…
RegExp正则函数 var reg = new RegExp("abc","igm"); //等价于 var reg = new RegExp(/abc/igm); //等价于 var reg = /abc/igm; //ES5不允许但ES6允许 var reg = new RegExp(/xyz/igm, 'i'); //等价于 var reg = /abc/i; U修饰符(用来处理码点大于FFFF的字符) /^\uD83D/u.test('\uD83D\uDC…
(1)语法说明:Generator函数其实是一个普通函数,其有两个特点,一是,function关键字与函数名之间有一个星号(*):二是Generator函数内部使用yield表达式,定义不同的状态,然后分段的去执行,如下:function * helloWorld() { yield "hello"; yield "world"; return "end";}// 说明:定义一个Generator函数helloWorld,此时Generator函…