我有一个MyClass接受对象作为方法附加到它的类。这些方法调用MyClass.push以将数据添加到对象的内部@_list数组。两个相同的测试用例中的第二个失败了,但我不明白为什么:
chai = require "chai"
chai.should()
obj =
  name: 'foo'
  f: ->
    for i in [1..5]
      @push "Element #{i}"
class MyClass
  constructor: (@options) ->
    @methods = {}
    @_list = []
    if 'attach' of @options
      @attach @options.attach
  push: (m) ->
    @_list.push m
  list: ->
    @_list
  method: (v) ->
    @methods[v].f
  run: (options) ->
    @method('foo')()
  attach: (o) ->
    @methods[o.name] = o
    @methods[o.name].f = o.f.bind(@)
describe 'MyClass', ->
  it 'list should have five elements', ->
    v = new MyClass attach: obj
    v.run()
    v.list().length.should.equal 5
  it 'list should have five elements (#2)', ->
    v = new MyClass attach: obj
    v.run()
    v.list().length.should.equal 5
我相信这与函数绑定有关,因为当我将第二个替换为v = new MyClass attach: obj:
v = new MyClass attach:
  name: 'foo'
  f: ->
    for i in [1..5]
      @push "Element #{i}"
两项测试均通过。但我不理解这种行为,因为我认为.bind()创建了函数的副本。那么这里发生了什么,我如何让两个测试独立运行?