如何在咖啡脚本的特定范围内声明变量? [英] How do I declare a variable in a specific scope in coffeescript?

查看:17
本文介绍了如何在咖啡脚本的特定范围内声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用 beforeEach 块的咖啡脚本中编写 jasmine 测试.这遇到了咖啡脚本的变量范围问题.以下是我想写的:

I'm trying to write a jasmine test in coffeescript that uses a beforeEach block. This runs into a problem with coffeescript's variable scoping. Here's what I'd like to write:

describe 'PhoneDetailCtrl', () ->
  beforeEach () ->
    scope = angular.scope()
    $browser = scope.$service('$browser')

  it 'should fetch phone detail', () ->
    scope.params = {phoneId:'xyz'}
    $browser.xhr.expectGET('phones/xyz.json').respond({name:'phone xyz'})
    ctrl = scope.$new(PhoneDetailCtrl)

    expect(ctrl.phone).toEqualData({})
    $browser.xhr.flush()

    expect(ctrl.phone).toEqualData({name:'phone xyz'})

但这不起作用,因为 scope$browser 将在最内部的范围内使用 var 进行声明.也就是说,一次在 beforeEach 中,然后再次在 it 块中.我可以通过初始化它们来强制在正确的范围内声明变量,但这似乎很奇怪:

This doesn't work, though, because the scope and $browser will get declared with var in the innermost scope. That is, once in the beforeEach and then again in the it block. I can force the variables to be declared in the right scope by initializing them, but this seems very strange:

describe 'PhoneDetailCtrl', () ->
  $browser = {}
  scope = {}
  beforeEach () ->
    scope = angular.scope()
    $browser = scope.$service('$browser')

  it 'should fetch phone detail', () ->
    scope.params = {phoneId:'xyz'}
    ...

这可行,但它编译成的javascript实际上是

This works, but the javascript it compiles to is actually

describe('PhoneListCtrl', function() {
  var $browser, ctrl, scope;
  $browser = {};
  ctrl = {};
  scope = {};

我只需要一行var $browser, ctrl, scope;.我可以在coffeescript中更简洁地写这个吗?

where all I need is the line var $browser, ctrl, scope;. Can I write this more concisely in coffeescript?

推荐答案

你做对了.

这在 CoffeeScript 文档中有描述.我不会担心它创建的 JS.是的,如果您自己编写它会有点混乱,但是当您使用像 CoffeeScript 这样的重写器时,这是您必须忍受的事情之一.

This is described in the CoffeeScript documentation. I wouldn't worry about the JS that it creates. Yes, it is a bit messy if you were to write it yourself, but this is one of the things that you have to live with when you use a re-writer like CoffeeScript.

不过,您确实有几个不错的选择.

You do, however, have a couple of options which are pretty nice.

如果你愿意,你可以把变量放在当前上下文中(这恰好是你的 jasmine.Spec 对象,所以它是一个相对安全和合适的地方放置变量......只是不要覆盖上下文中的现有变量.):

You can put the variables in the current context if you wish (which happens to be your jasmine.Spec object for the curious, so it is a relatively safe and appropriate place to be putting variables... just don't overwrite existing vars in the context.):

describe 'PhoneDetailCtrl', () ->
  beforeEach () ->
    @scope = angular.scope()
    @$browser = @scope.$service('$browser')

it 'should fetch phone detail', () ->
  @scope.params = {phoneId:'xyz'}
  #... etc

您还可以设置自己的变量来存储东西

You can also setup your own variable in which to store things

describe 'PhoneDetailCtrl', () ->
  setup = {}

  beforeEach () ->
    setup.scope = angular.scope()
    setup.$browser = setup.scope.$service('$browser')

  it 'should fetch phone detail', () ->
    setup.scope.params = {phoneId:'xyz'}
    #... etc

这篇关于如何在咖啡脚本的特定范围内声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆