我想在javascript中模拟类,并提出了这个方案......
var Random = function(a){ // class name used here
var Random = function(a){ // and used again here
this.valueOf = function(){
return Math.random()
}
this.under = function(limit){
return this*limit
}
this.floor = function(limit){
return Math.floor(this*limit)
}
}
return new Random(a)
}
var rnd = Random()
console.log(rnd*20<<0)
命名对我来说似乎很传统,TitelCase 中的类名和 camelCase 中的实例。我将该类包装两次,以便我可以返回一个实例,因为我不喜欢new Thing
在我的主代码中使用。
让我印象最不寻常的是对两个包装器使用相同的类名。这种模式有什么技术问题吗?
有没有更简单的方法来实现同样的事情?