属性不是函数异常

时间:2013-11-01 00:40:55

标签: javascript coffeescript

我是CoffeeScript的新手,我遇到了以下代码块的问题:

openShiftVariable = "foo"

class ServerEnvironment
    openShift: "OpenShift"
    cloud9: "Cloud9"
    environmentName: () -> 
        @openShift if openShiftVariable? else @cloud9 #fails
        #"#{@openShift}" if openShiftVariable? else "#{@cloud9}"
    constructor: () ->
        switch @environmentName()
            when @openShift
                console.log "OpenShift"
            when @cloud9
               console.log "Cloud9"

x = new ServerEnvironment()

同样在:http://jsfiddle.net/8NVqP/

我要做的是为openShift和cloud9定义一个常量,并在我的switch语句中使用它,以便我可以设置一些环境变量。我发现的是“environmentName”方法似乎不想返回一个字符串,我无法找到一个解决方法。

帮助表示赞赏!

编辑:2013年11月1日

事实证明,Cloud9在OpenShift中运行(doh!),这是上述工作的重写版本:

class ServerEnvironment
   openShift: "OpenShift"
   cloud9: "Cloud9"
   environmentName: () ->
       if process.env.C9_PROJECT? then @cloud9 else @openShift
   constructor: () ->
       console.log "Detecting server environment..."
       @appPath = process.cwd()

       switch @environmentName()
           when @openShift
               console.log "OpenShift detected!"
               @redisURL = "the redis URL"
               @mongoURL = "the mongo URL"
               @nodeJSPort = (Number) process.env.OPENSHIFT_NODEJS_PORT
               @nodeJSIP = process.env.OPENSHIFT_NODEJS_IP
           when @cloud9
               console.log "Cloud9 detected!"
               @redisURL = "the redis URL"
               @mongoURL = "the mongo URL"
               @nodeJSPort = (Number) process.env.PORT
               @nodeJSIP = process.env.IP

global.ENV = new ServerEnvironment()

1 个答案:

答案 0 :(得分:1)

更改:

@openShift if openShiftVariable? else @cloud9

有关:

if openShiftVariable? then @openShift else @cloud9