【发布时间】:2012-07-23 19:07:08
【问题描述】:
我在 coffeescript 中创建了一个类,该类使用 randomInt 方法生成 x 和 y 实例变量。但是,当我从此类创建对象时,x 和 y 值是不同的,但两者都是一致的。
这里是演示代码:http://jsfiddle.net/paulmason411/BvPBG/
class Shape
getRandomInt = (min, max) ->
Math.floor(Math.random() * (max - min + 1)) + min
y: getRandomInt(1,100)
x: getRandomInt(1,100)
shape1 = new Shape
shape2 = new Shape
alert(shape1.x)
alert(shape2.x)
alert(shape1.y)
alert(shape2.y)
我需要每个警报值都不同。
我搜索了一个解决方案,在其他编程语言中他们使用 srand() 但是 js 没有这个原生函数。
【问题讨论】:
标签: javascript object random coffeescript