我正在通过javscript平台使用dotnet语法创建一个js插件。
我想要实现的目标:生成一个随机数,如C# syntax。
看起来像这样:
(function (global) {
'use strict';
//require('babel-polyfill');
//let Int32 = require('./Int32'),
// ArgumentOutOfRangeException = require('./ArgumentOutOfRangeException');
let Int32 = function () { };
let ArgumentOutOfRangeException = function () { };
let Random = (function () {
let __data = {};
__data.Next = function (...args) {
switch (args.length) {
case 0:
return Math.floor(Math.random() * Int32.MaxValue);
case 1:
(function () {
let maxValue = args[0];
if (maxValue >= 0) {
return Math.floor(Math.random() * maxValue);
}
throw new ArgumentOutOfRangeException(`'maxValue' must be greater than or equal to 0.`, 'maxValue');
}());
case 2:
(function () {
let minValue = args[0];
let maxValue = args[1];
if (minValue <= maxValue) {
return Math.floor(Math.random() * (maxValue - minValue) + minValue);
}
throw new ArgumentOutOfRangeException(`'minValue' cannot be greater than maxValue.`, `minValue`);
}());
default:
throw `No overload for method 'Next' takes ${args.length} arguments`;
}
};
class Random {
constructor() {
//
}
set Next(value) {
return __data.Next;
}
get Next() {
return (...args) => __data.Next.call(this, ...args);
}
GetType() {
return 'System.Random';
}
}
return Random;
}());
//module.exports = Random;
global.Random = Random;
}(window));
let random = new Random();
let i = random.Next(100, 500);
console.log(i);
抛出此错误消息:
未捕获方法'Next'的重载没有超过2个参数
我的问题是:为什么不抓住这个案子?
case 2:
(function () {
let minValue = args[0];
let maxValue = args[1];
if (minValue <= maxValue) {
return Math.floor(Math.random() * (maxValue - minValue) + minValue);
}
throw new ArgumentOutOfRangeException(`'minValue' cannot be greater than maxValue.`, `minValue`);
}());
数组的长度恰好是2
,为什么我会收到错误?
谢谢!