- loop: function () {
- 'use strict';
- // The parameter of forEach() is an arrow function
- this.friends.forEach(friend => {
- // `this` is loop’s `this`
- console.log(this.name+' knows '+friend);
- });
- }
- beforeEach(function () {
- this.addMatchers({
- toBeInRange: function (start, end) {
- ...
- }
- });
- });
将如许的隐含参数变成明白的参数使得工作加倍明显,并且与箭头函数兼容。
与this相干的陷阱
this 在顶级感化域中
- beforeEach(api => {
- api.addMatchers({
- toBeInRange(start, end) {
- ...
- }
- });
- });
【编辑推荐】
- 2018第一季度编程说话排名:JavaScript 仍然位居榜首
- 带你懂得JavaScript反调试技能
- 1000多个项目中十大年夜JavaScript缺点以及若何避免
- 2018年,我们该若何应用JavaScript?
- 2018 年,前端开辟人员应当存眷哪些新晋技巧?
推荐阅读
有奖调研 | 人脸辨认功能在互联网行业认知度情况经由过程应用法度榜样级其余感知和洞察,为微办事带来编排和主动化的优势 微软的 Azure Service Fabric 的官方博客在2017.3.2>>>详细阅读
本文标题:JavaScript中this的运行机制及爬坑指南
地址:http://www.17bianji.com/lsqh/40750.html
1/2 1