Can I detect unused extra parameters passed to javascript methods?

Posted by Pablojim on Stack Overflow See other posts from Stack Overflow or by Pablojim
Published on 2012-06-17T20:13:08Z Indexed on 2012/06/17 21:16 UTC
Read the original article Hit count: 196

Filed under:

In Javascript I can call any method with more than the necessary amount of parameters and the extra parameters are silently ignored.

e.g.

letters = ['a','b','c']
//correct
letters.indexOf('a')
//This also works without error or warning
letters.indexOf('a', "blah", "ignore me", 38)

Are there ways to detect cases where this occurs?

My motivation is that in my experience cases where this occurs are usually bugs. Identification of these by code analysis or at runtime would help track these errors down.

These cases are especially prevalent where people are expecting alterations to base types which may not have occurred. Logging a warning where this happens

e.g.

Date.parse('02--12--2012', 'dd--MM--YYYY')

Notes: To be clear I would like a solution that doesn't involve me sprinkling checks all over my code and other peoples' code.

© Stack Overflow or respective owner

Related posts about JavaScript