我可以使用JS Lint,JS Hint或其他工具阻止将错误数量的参数传递给方法吗? [英] Can I prevent passing wrong number of parameters to methods with JS Lint, JS Hint, or some other tool?

查看:98
本文介绍了我可以使用JS Lint,JS Hint或其他工具阻止将错误数量的参数传递给方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript编程(以及一般的脚本语言)的新手,但是当我发生语法错误或意外声明全局变量时,我一直在使用JS Lint来帮助我。

I'm new to javascript programming (and scripting languages in general), but I've been using JS Lint to help me when I make syntax errors or accidentally declare a global variable.

然而,有一个JS Lint没有涵盖的情况,我认为这将是非常方便的。请参阅以下代码:

However, there is a scenario that JS Lint does not cover, which I feel would be incredibly handy. See the code below:

(function () {
    "use strict";
    /*global alert */

    var testFunction = function (someMessage) {
        alert("stuff is happening: " + someMessage);
    };

    testFunction(1, 2);
    testFunction();
}());

请注意,我将错误数量的参数传递给testFunction。我不会预见到我会故意遗漏一个参数或添加一个额外的参数。但是,JS Lint和JS Hint都不认为这是一个错误。

Notice that I am passing the wrong number of parameters to testFunction. I don't foresee myself ever in a situation where I would intentionally leave out a parameter or add an extra one like that. However, neither JS Lint nor JS Hint consider this an error.

是否有其他工具可以为我捕获这个?或者是否有一些理由说为什么传递这样的参数不应该被错误检查?

Is there some other tool that would catch this for me? Or is there some reason why passing parameters like that shouldn't be error checked?

推荐答案

这通常不适用于任何静态分析工具。这有几个原因:

This is not generally possible with any static analysis tool. There are several reasons for this:


  • 通常,JS函数可以接受任意数量的参数。

  • 大多数(全部?)linters一次只能处理一个文件。他们对其他文件中声明的函数一无所知。

  • 无法保证作为函数调用的属性是您期望的函数。请考虑以下代码段:

  • In general, JS functions can accept any number of arguments.
  • Most (all?) linters only work on a single file at a time. They do not know anything about functions declared in other files
  • There is no guarantee that a property being invoked as a function is the function that you expect. Consider this snippet:

var obj = { myFunc : function(a,b) { ... } };
var doSomething(x) { x.myFunc = function(a) { ... }; };
doSomething(obj);
obj.myFunc();


无法知道 myFunc 现在在调用 doSomething 后获取不同数量的args。

There is no way to know that myFunc now takes a different number of args after the call to doSomething.

JavaScript是一种动态语言,您应该接受并接受它。

JavaScript is a dynamic language and you should accept and embrace that.

而不是依靠linting来捕捉它本来不打算的问题我建议添加执行检查的函数的前提条件。

Instead of relying on linting to catch problems that it wasn't intended to I would recommend adding preconditions to your functions that does the check.

创建一个这样的辅助函数:

Create a helper function like this:

function checkThat(expression, message) {
  if (!expression) {
    throw new Error(message);
  }
}

然后像这样使用:

function myFunc(a, b) {
  checkThat(arguments.length === 2, "Wrong number of arguments.");

通过适当的单元测试,您永远不会在生产中看到此错误消息。

And with proper unit testing, you should never see this error message in production.

这篇关于我可以使用JS Lint,JS Hint或其他工具阻止将错误数量的参数传递给方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆