Class 编写一个CoffeeScript类

Class 编写一个CoffeeScript类,class,coffeescript,Class,Coffeescript,我有以下简单的CoffeeScript类: class Country constructor: (@val) -> console.log @val foreign: -> @val isnt "United States" domestic: -> not foreign() 我有这个简单的类来确定select下拉列表的一些逻辑 我这样称呼它: $country = new Country($val)

我有以下简单的CoffeeScript类:

class Country
    constructor: (@val) ->
        console.log @val
    foreign: ->
        @val isnt "United States"
    domestic: ->
        not foreign()
我有这个简单的类来确定select下拉列表的一些逻辑

我这样称呼它:

$country = new Country($val) if $('select[id*="country"]').val() > 0
console.log $country.foreign? if $country?
$val
正在('change')事件中设置<代码>$country.foreign?始终评估为true,即使我选择了美国以外的国家。我不知道我做错了什么。@val被设置为我要传递的值,但是外部函数不能正常工作

console.log $country.foreign()? if $country?
foreign
是一个函数调用

也就是说:

if (typeof $country !== "undefined" && $country !== null) {
  console.log($country.foreign() != null);
}
因此,您会得到一个关于返回值是否为空的日志,您可能希望删除

console.log $country.foreign() if $country?
应该是

console.log $country.foreign()? if $country?
foreign
是一个函数调用

也就是说:

if (typeof $country !== "undefined" && $country !== null) {
  console.log($country.foreign() != null);
}
因此,您会得到一个关于返回值是否为空的日志,您可能希望删除

console.log $country.foreign() if $country?

它应该是$country.foreign()?

它应该是$country.foreign()?

在这里对我很好:编辑-哦,我明白了…在这里对我很好:编辑-哦,我明白了…@dennismonsewicz尝试在console.log调用上删除
,就是这样!我一直忘了CoffeeScript中的问号是存在的…@dennismonsewicz尝试在console.log调用中删除
,就是这样!我总是忘记咖啡剧本中的问号是为了生存。。。