Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在mocha测试中,“this”在以下js arrow函数中指向什么?_Javascript_Ecmascript 6_Mocha.js_This - Fatal编程技术网

Javascript 在mocha测试中,“this”在以下js arrow函数中指向什么?

Javascript 在mocha测试中,“this”在以下js arrow函数中指向什么?,javascript,ecmascript-6,mocha.js,this,Javascript,Ecmascript 6,Mocha.js,This,这个指的是什么 const expect = require('chai').expect; const sinon = require('sinon'); describe('test', () => { describe('test()', () => { afterEach(function _test() { this.xxx = undefined; // What does `this` point here? }); });

这个指的是什么

const expect = require('chai').expect;
const sinon = require('sinon');

describe('test', () => {
  describe('test()', () => {
    afterEach(function _test() {
      this.xxx = undefined; // What does `this` point here?
    });
  });

  describe('test2()', () => {
    afterEach(function _test2() {
      this.yyy = undefined; // What does `this` point here?
    });
  });
});
顺便说一下,如果这两个
都指向同一个对象,那么在上述代码中使用
这个
是很好的

更新

代码

输出:

1 {}
4 {}


  test
2 {}
    going deeper
3 {}
6 {}
      ✓ has increased someVal to 3


  1 passing (4ms)
1 {}
4 {}


  test
2 {}
    going deeper
3 {}
      1) "before each" hook for "has increased someVal to 3"


  0 passing (6ms)
  1 failing

  1) test going deeper "before each" hook for "has increased someVal to 3":
     TypeError: Converting circular structure to JSON
      at Object.stringify (native)
      at formatValue (util.js:352:36)
      at inspect (util.js:186:10)
      at exports.format (util.js:72:24)
      at Console.log (console.js:43:37)
      at Context.<anonymous> (test3.js:32:15)
代码(将一个箭头功能更改为正常功能):

输出:

1 {}
4 {}


  test
2 {}
    going deeper
3 {}
6 {}
      ✓ has increased someVal to 3


  1 passing (4ms)
1 {}
4 {}


  test
2 {}
    going deeper
3 {}
      1) "before each" hook for "has increased someVal to 3"


  0 passing (6ms)
  1 failing

  1) test going deeper "before each" hook for "has increased someVal to 3":
     TypeError: Converting circular structure to JSON
      at Object.stringify (native)
      at formatValue (util.js:352:36)
      at inspect (util.js:186:10)
      at exports.format (util.js:72:24)
      at Console.log (console.js:43:37)
      at Context.<anonymous> (test3.js:32:15)
1{}
4 {}
测试
2 {}
深入
3 {}
1) 在每个“钩子”增加到3之前
0通过(6ms)
1失败
1) 在每个“钩子”的“钩子”增加到3之前,“测试更深”:
TypeError:将循环结构转换为JSON
at Object.stringify(本机)
at formatValue(util.js:352:36)
检查时(util.js:186:10)
at exports.format(util.js:72:24)
在Console.log(Console.js:43:37)
在上下文中。(test3.js:32:15)

这是一个很好的问题,因为很多箭头函数教程都没有提到这一点

箭头函数在定义时有“此”上下文绑定,但更重要的是您的问题。这将以递归方式继承

describe('test', () => {                 // `this` is the global object
  describe('test()', () => {             // `this` is the global object
    afterEach(function _test() {        
      this.xxx = undefined;              // `this` is the global object
    });
  });

  describe('test2()', () => {            // `this` is the global object
    afterEach(function _test2() {        
      this.yyy = undefined;              // `this` is the global object
    });
  });
});
这里有一个例子

constfoo=()=>{
log(“foo是窗口吗?”,this==窗口);
const a=(input)=>{console.log(“a是窗口吗?”,this==window);this.\input};
const b=()=>{console.log(“b是窗口吗?”,this===window);返回此。}
返回{a,b}
}
常数条=foo()
console.log(bar.b())//未定义
律师公会a(5)
console.log(bar.b())//5

console.log(window.\)//5
这是一个很好的问题,因为很多箭头函数教程都没有解决这个问题

箭头函数在定义时有“此”上下文绑定,但更重要的是您的问题。这将以递归方式继承

describe('test', () => {                 // `this` is the global object
  describe('test()', () => {             // `this` is the global object
    afterEach(function _test() {        
      this.xxx = undefined;              // `this` is the global object
    });
  });

  describe('test2()', () => {            // `this` is the global object
    afterEach(function _test2() {        
      this.yyy = undefined;              // `this` is the global object
    });
  });
});
这里有一个例子

constfoo=()=>{
log(“foo是窗口吗?”,this==窗口);
const a=(输入)=>{console.log(“a is window?”,this==窗口);这。u=input};
const b=()=>{console.log(“b是窗口吗?”,this===window);返回此。}
返回{a,b}
}
常数条=foo()
console.log(bar.b())//未定义
律师公会a(5)
console.log(bar.b())//5

console.log(window.)//5
在您查询的两个位置,
由Mocha绑定到内部Mocha对象。例如,这个对象允许您做的一件事就是更改Mocha的配置。类似于使用
this.timeout(newValue)
更改异步函数的超时。在下面的例子中,它不是特别有用,但摩卡可以很好地运行它。例如:

describe('test', () => {
  describe('test()', () => {
    afterEach(function _test() {
      this.timeout(5000);
      this.xxx = undefined;
    });

    it("foo", () => {});
  });

  describe('test2()', () => {
    afterEach(function _test2() {
      this.timeout(5000);
      this.yyy = undefined;
    });

    it("foo", () => {});
  });
});
请注意,如果要在每次
回调后为
使用箭头函数,则无法访问Mocha为回调设置的
的值。(
将在外部范围中设置一个值。)

顺便说一句,如果这两个(s)都指向同一个对象,那么在上述代码中使用它是很好的

我不建议在
上设置任意字段。不知道您设置的字段何时会与更新版本的摩卡引入的字段发生冲突。我总是能够在封闭的
description
中设置一个变量,并使用该变量:

describe('test', () => {
    let something;

    beforeEach(function _test() {
        something = fetchATestFixtureFromSomewhere();
    });

    it("foo", () => {
        // use the variable something here.
    });
});

关于你的更新。无论您在控制台上看到什么地方,这都是因为
{}
控制台.log中包含的所有函数都是箭头函数,正如我前面所说,如果使用箭头函数,则无法访问Mocha绑定到
this
的对象,因为在这种情况下
this
是从外部范围获取的,而不是从绑定到函数的值获取的。如果所有封闭函数都是箭头函数,则
来自最外层范围,且其值
{}
位于最外层范围


您得到的错误是因为您不能将Mocha分配给
this
的值转储到控制台,因为正如错误消息所示,它是一个带有循环引用的结构。

在您查询的两个位置,
this
由Mocha绑定到一个内部Mocha对象。例如,这个对象允许您做的一件事就是更改Mocha的配置。类似于使用
this.timeout(newValue)
更改异步函数的超时。在下面的例子中,它不是特别有用,但摩卡可以很好地运行它。例如:

describe('test', () => {
  describe('test()', () => {
    afterEach(function _test() {
      this.timeout(5000);
      this.xxx = undefined;
    });

    it("foo", () => {});
  });

  describe('test2()', () => {
    afterEach(function _test2() {
      this.timeout(5000);
      this.yyy = undefined;
    });

    it("foo", () => {});
  });
});
请注意,如果要在每次
回调后为
使用箭头函数,则无法访问Mocha为回调设置的
的值。(
将在外部范围中设置一个值。)

顺便说一句,如果这两个(s)都指向同一个对象,那么在上述代码中使用它是很好的

我不建议在
上设置任意字段。不知道您设置的字段何时会与更新版本的摩卡引入的字段发生冲突。我总是能够在封闭的
description
中设置一个变量,并使用该变量:

describe('test', () => {
    let something;

    beforeEach(function _test() {
        something = fetchATestFixtureFromSomewhere();
    });

    it("foo", () => {
        // use the variable something here.
    });
});

关于你的更新。无论您在控制台上看到什么地方,这都是因为
{}
控制台.log中包含的所有函数都是箭头函数,正如我前面所说,如果使用箭头函数,则无法访问Mocha绑定到
this
的对象,因为在这种情况下
this
是从外部范围获取的,而不是从绑定到函数的值获取的。如果所有封闭函数都是箭头函数,则
来自最外层范围,且其值
{}
位于最外层范围


您得到的错误是因为您不能将Mocha分配给
this
的值转储到控制台,因为正如错误消息所示,它是一个带有循环引用的结构。

这完全取决于它是什么测试框架。它是设置这个
的东西,因为它是调用函数的东西