Domanda

I've just started using JSlint to see if I can improve my code. It's quite...pedantic but very useful.

I currently have the issue whereby a function is being called but jslint is telling me it's not yet been defined.

The function is a simple generate random number function and it's stored in a generic 'helper file'. I don't want to or see the need in copying this function into every file where it's called, kinda defeats the purpose of the helper file :)

Is there something else that I'm missing? I can't help but think I'm making an amateur mistake but can't see it for looking.

The error is being presented on this line : randomNum = random_num(layerDetails.length);

and the random_num function is simply:

function random_num($value) { $result = Math.floor(Math.random() * $value); return $result; }

thanks for any help. I'd like jslint to give me clean bill of health so to speak.

È stato utile?

Soluzione

You have a global function which is also a global variable. Globals are generally a bad thing because they eat up your namespace and cause naming collisions.

If you're not worried about that you can let JSLint know which globals you are using by adding the following comment to the top of each file you lint:

/*globals random_num, $ */

I've added $ to demonstrate that you can provide a list of globals.

However since you're using Node I'd recommend adding this to a Utils module. JSLint will not complain about that since you'll be declaring the module with var.

utils.js

module.exports = {
  random_num: function random_num($value) {
    $result = Math.floor(Math.random() * $value);
    return $result;
  }
};

Elsewhere:

var utils = require('./utils');
utils.random_num();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top