25

My code:
note: the Slider Object is declared but omitted in the snippet below for better readability

"use strict";
/*global arrayContainer, SliderInstance, DomObjects */
arrayContainer = new Slider.constructArray();
SliderInstance = Object.beget(Slider);
DomObjects = {

    animationContainer: document.getElementById('animationContainer'),
    buttonRight: document.getElementById('buttonRight'),
    buttonRightDots: document.getElementById('buttonRightDots'),
    ieEffectImg: document.getElementById('ie_effectIMG')        
};


This is what JSLint produces (and on the other two Objects SliderInstance and DomObjects)

Error:
Problem at line 3 character 1: Read only.

arrayContainer = new Slider.constructArray();

Problem at line 3 character 1: Stopping. (27% scanned).


How do I satisfy JSLint's requirements? What does "Read only." mean?

Jay Wick
  • 12,325
  • 10
  • 54
  • 78
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147

2 Answers2

49

Try this:

 /*global arrayContainer:true, SliderInstance:true, DomObjects:true, document, Slider*/

Informs JSLint that these globals are assigned intentionally.

glebm
  • 20,282
  • 8
  • 51
  • 67
  • 1
    You've got a keen eye and good understanding of JSLint. That worked nicely. JSLint Docs say: "Each name can optionally be followed by a colon and either true or false, true indicated that the variable may be assigned to by this file, and false indicating that assignment is not allowed which is the default." Some interesting questions arise out of this answer. 1) Why doesn't it matter what boolean I assign to the global document object? And more importantly: 2) Why does JSLint or JavaScript care about -where- I assign global objects? – Stephan Kristyn Sep 13 '10 at 23:22
  • 2
    Oh, this is to avoid assigning global object by accident (e.g. forgetting the var keyword and writing `document = $('#attachment_document')` – glebm Sep 14 '10 at 14:41
  • 1
    Also, JavaScript itself does not care where you assign those, only JSLint does. – glebm Sep 14 '10 at 14:54
  • FYI make sure there is no space after /* or before the closing */, spent ages trying to work out why the directive was not being used. – rob Sep 22 '14 at 10:48
6

use

/*global arrayContainer:true, SliderInstance:true, DomObjects:true */

see doco under 'Global Variables' - the 'true' says that this file can assign to those variables.

Luke Schafer
  • 9,209
  • 2
  • 28
  • 29