Can I prevent passing wrong number of parameters to methods with JS Lint, JS Hint, or some other tool?

本小妞迷上赌 提交于 2019-12-12 10:46:03

问题


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.

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();
}());

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?


回答1:


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

  • 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();
    

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

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

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);
  }
}

Then use it like this:

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.




回答2:


It's not natively possible in javascript. You would have to do something like this:

var testFunction = function (someMessage) {
    var args = Array.prototype.slice.call(arguments);
    if (args.length !== 1) throw new Error ("Wrong number of arguments");
    if (typeof args[1] !== string)  throw new Error ("Must pass a string");
    // continue
};



回答3:


Paul Irish demoed a hack for this a while back...passing undefined at the end of the parameters...

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

He demos it here...see if it's what your looking for.



来源:https://stackoverflow.com/questions/31195952/can-i-prevent-passing-wrong-number-of-parameters-to-methods-with-js-lint-js-hin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!