Node.js Coffeescript resetCards不是一个函数
我一直在nodejs上运行coffeescript和expressjs,我正在制作一个小脚本来给你9张随机的扑克牌(没有重复),我制作了一个函数resetCards来在每次显示后重置扑克牌,但是当我运行脚本时,它给了我:Node.js Coffeescript resetCards不是一个函数,node.js,express,compiler-errors,coffeescript,Node.js,Express,Compiler Errors,Coffeescript,我一直在nodejs上运行coffeescript和expressjs,我正在制作一个小脚本来给你9张随机的扑克牌(没有重复),我制作了一个函数resetCards来在每次显示后重置扑克牌,但是当我运行脚本时,它给了我: TypeError: resetCards is not a function at Object.<anonymous> (/home/zunon/projects/xKoot/router.js:10:1) at Module._compile
TypeError: resetCards is not a function
at Object.<anonymous> (/home/zunon/projects/xKoot/router.js:10:1)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/zunon/projects/xKoot/xkoot.js:6:10)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
TypeError:resetCards不是一个函数
反对。(/home/zunon/projects/xKoot/router.js:10:1)
在模块处编译(Module.js:398:26)
在Object.Module.\u extensions..js(Module.js:405:10)
在Module.load(Module.js:344:32)
在Function.Module.\u加载(Module.js:301:12)
at Module.require(Module.js:354:17)
根据需要(内部/module.js:12:17)
反对。(/home/zunon/projects/xKoot/xKoot.js:6:10)
在模块处编译(Module.js:398:26)
在Object.Module.\u extensions..js(Module.js:405:10)
这是文件router.coffee:
express = require 'express'
router = express.Router()
cards = []
resetCards()
router.route '/randomcard'
.get (req, res) ->
cardNames = []
for i in [1..9] by 1
cardNames[i] = createCardName()
console.log cardNames
res.render 'randomcard', {cardNames}
return
createCardName = ->
position = Math.floor Math.random() * cards.length
cards.splice position, 1
cards[position]
resetCards = ->
for i in [1..13] by 1
cards[i - 1] = "club#{i}"
cards[i + 12] = "dmnd#{i}"
cards[i + 25] = "hart#{i}"
cards[i + 38] = "spad#{i}"
if i < 3
cards[i + 51] = "joke#{i}"
module.exports = router
express=需要“express”
路由器=express.router()
卡片=[]
重置卡片()
router.route'/randomcard'
.get(请求、恢复)->
名片=[]
因为我在[1..9]中以1
cardNames[i]=createCardName()
console.log卡名
res.render'randomcard',{cardNames}
返回
createCardName=->
position=Math.floor Math.random()*cards.length
卡。拼接位置,1
卡片[位置]
重置卡片=->
因为我在[1..13]中以1
卡片[i-1]=“俱乐部{i}”
卡片[i+12]=“dmnd{i}”
卡片[i+25]=“哈特{i}”
卡片[i+38]=“spad{i}”
如果我<3
卡片[i+51]=“笑话{i}”
module.exports=路由器
CoffeeScript不像JavaScript那样将函数提升到作用域的顶端。在JavaScript中,如果您说:
f();
function f() { }
它将起作用,因为f
的定义被提升到顶部,因此代码相当于:
function f() { }
f();
然而,CoffeeScript只是将声明提升到顶部,而不是定义。所以,当你在《咖啡脚本》中这样说的时候:
f()
f = ->
f = ->
在JavaScript中看起来是这样的:
var f;
f();
f = function() { };
var f;
f = function() { };
因此,当调用f
时,它是未定义的
,您会得到一个类型错误
解决方案是将您的resetCards()
调用放在resetCards
的定义下面:
resetCards = ->
for i in [1..13] by 1
cards[i - 1] = "club#{i}"
cards[i + 12] = "dmnd#{i}"
cards[i + 25] = "hart#{i}"
cards[i + 38] = "spad#{i}"
if i < 3
cards[i + 51] = "joke#{i}"
resetCards()
与此JavaScript相同:
var f;
f();
f = function() { };
var f;
f = function() { };
但这与:
function f() { }
CoffeeScript中没有与函数f(){}等价的函数。谢谢,这是有效的,我习惯于在jquery中使用$()函数下面的函数,我不熟悉node、express和CoffeeScript,所以我想我必须习惯使用带有变量的函数。