Hudson Taylor
Hudson Taylor

Reputation: 1162

JSLint Object Error

I am using JSLint to lint the following code:

'use strict';

var mathService = {
   add: add,
   subtract: subtract,
   multiply: multiply,
   divide: divide,
   power: power,
   squareRoot: squareRoot
};

function add(first, second) {
   return first + second;
}

function subtract(first, second) {
   return first - second;
}

function multiply(first, second) {
   return first * second;
}

function divide(first, second) {
   return first / second;
}

function power(first, second) {
   return Math.pow(first, second);
}

function squareRoot(first) {
   return Math.sqrt(first);
}

When I try to lint this code, I get an error message for each property in my object indicating that it is undefined. However, I did not think one had to define object properties? Thanks in advance for the help!

Upvotes: 0

Views: 64

Answers (1)

Clyde Lobo
Clyde Lobo

Reputation: 9174

Move the object after the functions, like

"use strict";

function add(first, second) {
   return first + second;
}

function subtract(first, second) {
   return first - second;
}

function multiply(first, second) {
   return first * second;
}

function divide(first, second) {
   return first / second;
}

function power(first, second) {
   return Math.pow(first, second);
}

function squareRoot(first) {
   return Math.sqrt(first);
}

var mathService = {
   add: add,
   subtract: subtract,
   multiply: multiply,
   divide: divide,
   power: power,
   squareRoot: squareRoot
};

You would get some warnings, but no error.

Upvotes: 2

Related Questions