1
我是新來的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/
我想什麼do是爲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()
的'SWITC h'工作得很好:http://jsfiddle.net/ambiguous/T2WyR/ –
@ muistooshort嗯,你是對的,不知何故,我一直認爲這是不可能的。 – plalx
另外,'如果openShiftVariable?'和'if openShiftVariable'具有完全不同的語義。比較http://jsfiddle.net/ambiguous/Bq8Cw/和http://jsfiddle.net/ambiguous/fTNzN/ –