当前位置: 首页 > news >正文

ES6之函数的扩展二

ES6之函数的扩展一 传送门

9.3 函数length属性

函数的length属性,不包含rest参数

console.log((function (a) {}).length) // 1
console.log((function (...a) {}).length) // 0
console.log((function (a1,b,...a) {}).length) // 2

10:严格模式

在 ES5 中,允许函数内部显示定义 严格模式 ‘use strict’
ES6 中,函数参数使用了参数默认值,对象解构参数和扩展运算符的话,如果在函数内部显示定义了严格模式,会出现错误

10.1 函数中定义严格模式产生错误

10.1.1 case 1: 参数默认值
function fun101 (a,b=a) {'use strict'console.log(a,b)
}
fun101(12,24) // Uncaught SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list
10.1.2 case 2: 对象解构
function fun102({a,b=1}) {'use strict'console.log(a,b) // Uncaught SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list
}
fun102(2,6)
10.1.3 case 3: rest 扩展运算符
function fun103 (...items) {'use strict'console.log(items) // Uncaught SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list 
}
fun103(1,2,3)

流程一:函数体中的严格模式,是适用于函数体和函数参数;
流程二:函数执行顺序是先从上往下,先执行函数参数,再执行函数体;
流程三:产生的错误:'use strict’在函数体声明的;
流程四:但是函数参数不知道当前是以严格模式运行。(这时候函数参数根据变量作用域,已经运行结束),就会产生错误

  • 注:为了避免如此复杂的行为,标准协议禁止了这种用法,在使用参数默认值,对象解构参数和扩展运算符的情况下,不能显示指定严格模式

10.2 避免严格模式错误

10.2.1 case 1 在函数外部定义 严格模式 全局
'use strict'
function fun104(x,y=x){console.log(y) // 10
}
fun104(10) 
10.2.2 case 2 函数嵌套 中使用 严格模式
const fun105 = (x) => {console.log(x)return function fun (x,y=x) {console.log(y) // 8}fun(x)
}
fun105(8)

11: name 属性

11.1 case 1

function fun1101(){}
console.log(fun1101.name) // fun1101

11.2 匿名函数

返回变量名

const fun1102 = function () {}
console.log(fun1102.name) // fun1102

11.3 具名函数

返回实际函数名

const fun1103 = function bar () {}
console.log(fun103.name) // fun1103

11.4 构造函数

console.log((new Function).name) // anonymous

11.5 bind方法

修改this指向

function fun1104 () {}
console.log(fun1104.bind({}).name) // bound fun1104
console.log((function(){}).bind({}).name) // bound

12 箭头函数

如果箭头函数代码块部分多余一条语句,需要使用大括号包括起来,并用return进行返回

12.1 case 1

let fun1201 = () => { a1: 1 }
console.log(fun1201()) // undefined

12.2 简化函数

let arr = [1,2,3,4,5,6]
let arr1 = arr.map((x) => x * 2)
console.log('arr1:',arr1) // [2, 4, 6, 8, 10, 12]

12.3 sort 排序

let values = [1,6,8,3,5,6,3,2,4,6]
let sort1 = values.sort(( a,b ) => a-b)
console.log(sort1) // [1, 2, 3, 3, 4, 5, 6, 6, 6, 8]

12.4 rest参数与箭头函数

let arr1204 = (...nums) => nums
console.log(arr1204(1,2,3,4,5,6,7,8)) // [1, 2, 3, 4, 5, 6, 7, 8]

rest形式的参数本身就是一个数组

http://www.lryc.cn/news/188888.html

相关文章:

  • Ubuntu-Ports更新源 ARM64更新源
  • 渗透测试怎么入门?(超详细解读)
  • MS31804四通道低边驱动器可pin对pin兼容DRV8804
  • Fastadmin 子级菜单展开合并,分类父级归纳
  • Idea创建springboot工程的时候,发现pom文件没有带<parent>标签
  • element树形控件编辑节点组装节点
  • 【算法-动态规划】斐波那契第 n 项
  • Linux系统运行级别详解,切换、配置和常见服务
  • 企业需要ERP系统的八大理由,最后一个尤其重要
  • Java-Atomic原子操作类详解及源码分析,Java原子操作类进阶,LongAdder源码分析
  • 算法通过村第十二关-字符串|黄金笔记|冲刺难题
  • 3ds Max渲染太慢?创意云“一键云渲染”提升3ds Max渲染体验
  • 记录一次公益SRC的常见的cookie注入漏洞(适合初学者)
  • [ACTF2020 新生赛]Exec1
  • DeepFace【部署 03】轻量级人脸识别和面部属性分析框架deepface在Linux环境下服务部署(conda虚拟环境+docker)
  • vuex的求和案例和mapstate,mapmutations,mapgetters
  • Docker 网络访问原理解密
  • 统信UOS离线安装nginx
  • 机器学习基础-手写数字识别
  • idea 插件推荐(持续更新)
  • 实现Promise所有核心功能和方法
  • 学习总结1
  • 使用 Apache Camel 和 Quarkus 的微服务(二)
  • pid-limit参数实验
  • jvm--执行引擎
  • day13|二叉树理论
  • php+html+js+ajax实现文件上传
  • 日期时间参数,格式配置(SpringBoot)
  • JAVA 泛型的定义以及使用
  • Day-08 基于 Docker安装 Nginx 镜像-负载均衡