2

假设我定义了一个函数,例如:

var x = function (options, callback) { /* ... */ }

options需要有属性fooand bar, wherefoo应该是 type number, andbar是 type string

因此,基本上,我可以使用以下代码进行检查:

var x = function (options, callback) {
  if (!options) { throw new Error('options is missing.'); }
  if (!options.foo) { throw new Error('foo is missing.'); }
  if (!options.bar) { throw new Error('bar is missing.'); }
  if (!callback) { throw new Error('callback is missing.'); }
  // ...
}

但这仅检查是否存在,尚未检查正确的类型。当然,我可以添加进一步的检查,但这很快就会变得冗长,而且可读性不太好。一旦我们开始谈论可选参数,它就会变得一团糟,参数转换等等……</p>

处理这个问题的最佳方法是什么(假设你想检查它)?

更新

澄清我的问题:我知道有typeof运算符,我也知道如何处理可选参数。但是我必须手动进行所有这些检查,而这——当然——不是我们能想到的最好的。

我的问题的目标是:是否有一个现成的函数/库/任何你可以告诉你期望五个特定类型的参数,一些是强制性的,一些是可选的,以及函数/库/无论做什么检查和为您绘制地图,以便将这一切归结为单线?

基本上,例如:

var x = function (options, callback) {
  verifyArgs({
    options: {
      foo: { type: 'number', mandatory: true },
      bar: { type: 'string', mandatory: true }
    },
    callback: { type: 'function', mandatory: false }
  });
  // ...
};
4

7 回答 7

1

javascript有typeof

console.log( typeof '123' ); // string
console.log( typeof 123 ); // number
console.log( typeof undefinedVar); // undefined


var x = function (options, callback) {
  if (typeof options =='undefined' { throw new Error('options is missing.'); }
  if (typeof options.foo !='number') { throw new Error('foo is missing.'); }
  if (typeof options.bar !='string') { throw new Error('bar is missing.'); }
  if (typeof callback!= 'function') { throw new Error('callback is missing.'); }
  // ...
}
于 2013-08-26T07:57:49.897 回答
1

你可以这样做:

var x = function (options, callback) {
    var options = options || {};
    if (typeof options.foo != 'number') {
        throw new Error('foo need to be a number.');
    }
    if (typeof options.bar != 'string') {
        throw new Error('bar need to be a string.');
    }
    // ...
}
于 2013-08-26T07:58:49.107 回答
0

使用类型

// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
typeof Number(1) === 'number'; // but never use this form!

...

于 2013-08-26T07:58:54.307 回答
0
if (myObj.hasOwnProperty(myProp)) { //to check whether mentioned property exist
    if (typeof myObj[myProp] == "string") { //or integer
        // your code to be executed!!!
    } else {
        //ERROR!!!
    }
}
于 2013-08-26T08:00:20.323 回答
0

请参阅以下示例以检查类型、字符串或整数:

if(typeof(somevar) === 'string') alert('string');
if(typeof(somevar)==="number" && Math.round(somevar) == somevar) alert('is int');
于 2013-08-26T08:02:45.853 回答
0

虽然这并不完全是 javascript 类型模型背后的想法,但我通常使用for-loop 来迭代我的 to-check-object 的数据成员:

/* (.startsWith has to be implementes somehow) */
for (key in my_object_with_data) {
    if (!key.startsWith(typeof my_object_with_data[key]+"_") {
        /* The member "key" has wrong type. */
    }
}

必须命名对象的成员,number_a例如检查它的类型是否为数字。

可以通过一些通用的检查方法来完成额外的验证,这些检查方法可以根据typeof值在 for 循环中应用。

于 2013-08-26T08:20:14.040 回答
0

好的,我找到了一个可以自己做我想做的事的库,它叫做ArgueJS

function range(){ 
  arguments = __({start: [Number, 0], stop: Number, step: [Number, 1]})

  for(var i = arguments.start; i < arguments.stop; i += arguments.step)
    console.log(i);
}
于 2013-08-26T08:53:23.600 回答